1

这是 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 个文件,并按指定日期范围内的资产编号分组

第二,我从另一个表格中提取日期范围内已签发的金额总和第三,我从另一个表格中提取日期范围内的金额总和

对于输出,我想要第一个表,然后是 2 个额外的列 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

求 2

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

求 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
4

2 回答 2

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
于 2012-09-05T11:36:54.147 回答
0

据我所知,您并没有像其他人建议的那样寻找 UNION,而是需要 3 行才能有 2 个额外的列显示每个资产编号的 snissued 和 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

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

于 2012-09-05T11:50:23.543 回答