日期:2014-05-17 浏览次数:20570 次
if object_id('Tempdb..#A') is not null drop table #A
if object_id('Tempdb..#B') is not null drop table #B
create table #A(
[RQ] VARCHAR(8) null,
[SL] int null,
[MC] VARCHAR(8) null,
[TH] VARCHAR(10)
)
create table #B(
[RQ] VARCHAR(8) null,
[SL] int null,
[MC] VARCHAR(8) null,
[TH] VARCHAR(10)
)
INSERT INTO #A
select '20130502',1,'AAA','1-1' union all
select '20130502',1,'BBB','1-2' union all
select '20130502',1,'CCC','1-3' union all
select '20131012',1,'AAA','1-1' union all
select '20131012',1,'BBB','1-2' union all
select '20131013',1,'DDD','1-4'
INSERT INTO #B
select '20130502',1,'AAA','1-1' union all
select '20130502',1,'CCC','1-3' union all
select '20130503',1,'BBB','1-2' union all
select '20131012',1,'AAA','1-1'
declare @rq varchar(8)
set @rq='20130502'
select a.RQ,sum(isnull(a.SL,0)) AS ASum,SUM(isnull(b.SL,0)) as BSum from #A a left join #B b on a.RQ=b.RQ and a.MC=b.MC and a.T