一个两表数据比较后的输出语句

mailto520 2008-12-12 08:38:51
table1
PlanID InAmount InDate
081210001 50 2008-12-9
081210002 80 2008-12-8
081211001 20 2008-12-10
081211002 30 2008-12-11

table2
PlanID OutAmount OutDate
081210001 10 2008-12-10
081210002 40 2008-12-10
081210001 30 2008-12-11
081211001 10 2008-12-11
081210001 10 2008-12-11

result TB1
PlanID InAmount InDate
081210002 80 2008-12-8
081211001 20 2008-12-10
081211002 30 2008-12-11
说明:table2里的数量按PlanID汇总,和table1比较,输出毛table1 inAmount>table2 sum(OutAmount)的内容,table1里的数量不需要汇总,


...全文
57 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mailto520 2008-12-12
  • 打赏
  • 举报
回复
谢谢高手
水族杰纶 2008-12-12
  • 打赏
  • 举报
回复
set nocount on
if object_id('table1')is not null drop table table1
go
create table table1(PlanID varchar(10), InAmount int, InDate datetime)
insert table1 select '081210001' , 50 , '2008-12-9'
insert table1 select '081210002' , 80 , '2008-12-8'
insert table1 select '081211001', 20 , '2008-12-10'
insert table1 select '081211002', 30 , '2008-12-11'
if object_id('table2')is not null drop table table2
go
create table table2(PlanID varchar(10), OutAmount int, OutDate datetime)
insert table2 select '081210001' , 10, '2008-12-10'
insert table2 select '081210002' , 40 , '2008-12-10'
insert table2 select '081210001' , 30 , '2008-12-11'
insert table2 select '081211001' , 10 , '2008-12-11'
insert table2 select '081210001', 10 , '2008-12-11'
select a.* from table1 a inner join(select PlanID,sum(OutAmount)OutAmount from table2 group by planid)b on a. InAmount>=b.OutAmount and a.planid=b.planid
/*PlanID InAmount InDate
---------- ----------- ------------------------------------------------------
081210001 50 2008-12-09 00:00:00.000
081210002 80 2008-12-08 00:00:00.000
081211001 20 2008-12-10 00:00:00.000*/
dawugui 2008-12-12
  • 打赏
  • 举报
回复
create table table1(PlanID   varchar(20),   InAmount int,     InDate datetime)
insert into table1 values('081210001' , 50 , '2008-12-9')
insert into table1 values('081210002' , 80 , '2008-12-8')
insert into table1 values('081211001' , 20 , '2008-12-10')
insert into table1 values('081211002' , 30 , '2008-12-11')
create table table2(PlanID varchar(20), OutAmount int, OutDate datetime)
insert into table2 values('081210001' , 10 , '2008-12-10')
insert into table2 values('081210002' , 40 , '2008-12-10')
insert into table2 values('081210001' , 30 , '2008-12-11')
insert into table2 values('081211001' , 10 , '2008-12-11')
insert into table2 values('081210001' , 10 , '2008-12-11')

select PlanID , InAmount , InDate from
(
select m.* , m.InAmount - isnull((select sum(OutAmount) from table2 n where n.PlanID = m.PlanID),0) val from table1 m
) t
where val > 0

drop table table1 , table2
/*
PlanID InAmount InDate
-------------------- ----------- ------------------------------------------------------
081210002 80 2008-12-08 00:00:00.000
081211001 20 2008-12-10 00:00:00.000
081211002 30 2008-12-11 00:00:00.000

(所影响的行数为 3 行)

*/
dawugui 2008-12-12
  • 打赏
  • 举报
回复
select m.* , m.InAmount - isnull((select sum(OutAmount) from table2 n where n.PlanID = m.PlanID),0) from table1 m 

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧