从两个表里汇总数量同时显示出来,sql语句如何写?
有两个表:
A: RQ,SL,MC,TH ....
20130502 1 AAA 1-1
20130502 1 BBB 1-2
20130502 1 CCC 1-3
20131012 1 AAA 1-1
20131012 1 BBB 1-2
20131013 1 DDD 1-4
B:RQ,DJSL,MC,TH....
20130502 1 AAA 1-1
20130502 1 CCC 1-3
20130503 1 BBB 1-2
20131012 1 AAA 1-1
我现在需要一条sql语句按日期统计查询,统计这两个表日期相同的那天数量的合计数
我查询20130502
则显示
RQ SUM(A.SL) SUM(B.SL)
20130502 3 2
查询20131012
RQ SUM(A.SL) SUM(B.SL)
20131012 2 1
查询20131013
RQ SUM(A.SL) SUM(B.SL)
20131013 1
[解决办法]
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.TH=b.TH
WHERE a.RQ=@RQ
GROUP BY a.RQ
--------------------------
(6 行受影响)
(4 行受影响)
RQ ASum BSum
-------- ----------- -----------
20130502 3 2
(1 行受影响)
if object_id('A') is not null
drop table A
go
if object_id('B') is not null
drop table B
go
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'
go
--查询20130502
select rq,
sum(case when flag = 'A' then sl else 0 end) as 'SUM(A.SL)',
sum(case when flag = 'B' then sl else 0 end) as 'SUM(B.SL)'
from
(
select rq,sl,'A' as flag from a
union all
select rq,sl,'B' as flag from b
)t
where t.rq = '20130502'
group by rq
/*
rq SUM(A.SL) SUM(B.SL)
-------- ----------- -----------
20130502 3 2
*/
--查询 20131012
select rq,
sum(case when flag = 'A' then sl else 0 end) as 'SUM(A.SL)',
sum(case when flag = 'B' then sl else 0 end) as 'SUM(B.SL)'
from
(
select rq,sl,'A' as flag from a
union all
select rq,sl,'B' as flag from b
)t
where t.rq = '20131012'
group by rq
/*
rq SUM(A.SL) SUM(B.SL)
-------- ----------- -----------
20131012 2 1
*/
--查询 20131013
select rq,
sum(case when flag = 'A' then sl else 0 end) as 'SUM(A.SL)',
sum(case when flag = 'B' then sl else 0 end) as 'SUM(B.SL)'
from
(
select rq,sl,'A' as flag from a
union all
select rq,sl,'B' as flag from b
)t
where t.rq = '20131013'
group by rq
/*
rq SUM(A.SL) SUM(B.SL)
-------- ----------- -----------
20131013 1 0
*/