将3个SQL查询连接到一个结果集中

时间:2012-09-05 11:32:55

标签: sql join

这里有3个查询,重要的是所有三个查询都基于日期范围,需要匹配qry 1 assestnumber与snissued和snredeemed。提前谢谢

select 
    assetnumber, sum(mocalc),sum(micalc), sum(cocalc),sum(cicalc)
from 
    drops
where  
    dropdate > '09/01/2012' 
    and dropdate < dateadd(hour,-0,getdate())
group by 
    assetnumber

select 
    snissued,sum(amount) 
from 
    tickets
where  
    dateissued > '09/01/2012' 
    and dateissued < dateadd(hour,-0,getdate())
group by 
    snissued

select 
    snredeemed,sum(amount) 
from 
    tickets
where  
    dateredeemed > '09/01/2012' 
    and dateredeemed < dateadd(hour,-0,getdate())
group by 
    snredeemed

在第一篇中我总结了我使用的4个文件并按资产编号分组指定的日期范围

第二,我从另一张桌子中抽出了为日期范围散发的金额 第三,我从另一张表中提取了snredeemed前日期范围的金额总和

对于输出我想要第一个表然后另外2个colums snissued(总和金额)和snredeemed(总金额)

这是当前输出

qry 1
0-2459-36182    843 869 14  40
0-2621-31924    64700   86900   3000    25200
0-2739-41401    5702    5738    204 236

qry 2

0-2459-36182    162800
0-2621-31924    145000
0-2739-41401    298200

qry 3

0-2459-36182    157200
0-2621-31924    157800
0-2739-41401    292800

这是我想要的结果

0-2459-36182    843     869     14      40     162800  157200  
0-2621-31924    64700   86900   3000    25200  145000  157800
0-2739-41401    5702    5738    204     236    298200  292800

2 个答案:

答案 0 :(得分:1)

select 
    q1.*, snissuedsum, snredeemedsum
from
(
    select assetnumber, sum(mocalc) q11,sum(micalc) q12, sum(cocalc) q13,sum(cicalc) q14
    from drops 
    where  dropdate > '09/01/2012' 
    and dropdate < dateadd(hour,-0,getdate()) 
    group by assetnumber 
) q1
left join
(
    select snissued,sum(amount) snissuedsum 
    from tickets 
    where  dateissued > '09/01/2012' 
    and dateissued < dateadd(hour,-0,getdate()) 
    group by snissued 
) q2
  on q1.assetnumber = q2.snissued
  left join
(     
    select snredeemed,sum(amount) snredeemedsum
    from tickets 
    where  dateredeemed > '09/01/2012' 
    and dateredeemed < dateadd(hour,-0,getdate()) 
    group by snredeemed 
) q3
  on q1.assetnumber = q3.snredeemed

答案 1 :(得分:0)

从我收集的内容来看,你并不像其他人所建议的那样寻找UNION,但需要3行才能有2个额外的列显示每个资产数量的snissde和snredeemed。那是对的吗? 如果是这样,所有3的左连接类似于此:

select DRPS.assetnumber, DRPS.MOCALCSUM,DRPS.MICALCSUM,
    DRPS.COCALCSUM,DRPS.CICALCSUM,ISSUED.TotalIssued,REDEEMED.TotalRedeemed
from (select assetnumber, sum(mocalc) AS [MOCALCSUM],sum(micalc) AS [MICALCSUM],
        sum(cocalc) AS [COCALCSUM],sum(cicalc) AS [CICALCSUM]
        from drops
        where  dropdate > '09/01/2012' and dropdate < dateadd(hour,-0,getdate())
        group by assetnumber) AS DRPS
LEFT JOIN (select snissued,sum(amount) AS [TotalIssued] from tickets
    where  dateissued > '09/01/2012' and dateissued < dateadd(hour,-0,getdate())
    group by snissued) AS ISSUED
    ON DRPS.assetnumber=ISSUED.snissued
LEFT JOIN (select snredeemed,sum(amount) AS [TotalRedeemed] from tickets
    where  dateredeemed > '09/01/2012' and dateredeemed < dateadd(hour,-0,getdate())
    group by snredeemed) AS REDEEMED
    ON DRPS.assetnumber=REDEEMED.snredeemed

希望是对的!发布任何错误,我会纠正。