1

前几天在这方面得到了一些帮助,现在真的想做出更好的改变

这是代码

select 
    machines.serialnum, DRPS.assetnumber, DRPS.MOCALCSUM, DRPS.MICALCSUM,
    DRPS.COCALCSUM, DRPS.CICALCSUM, ISSUED.TotalIssued,
    REDEEMED.TotalRedeemed,drps.dropdate1,drps.dropdate2
from
    (select serialnum
     from machineinfo) as machines
LEFT JOIN 
    (select 
        assetnumber, min(dropdate) as [dropdate1], max(dropdate) as [dropdate2], 
        sum(mocalc) AS [MOCALCSUM], sum(micalc) AS [MICALCSUM],
        sum(cocalc) AS [COCALCSUM],sum(cicalc) AS [CICALCSUM]
     from drops
     where dropdate > '09/04/2012' and dropdate < dateadd(hour, -0, getdate())
     GROUP BY assetnumber) AS DRPS on machines.serialnum = drps.assetnumber
LEFT JOIN 
    (select 
        snissued, cast(sum(amount) as money) / 100 AS [TotalIssued] 
     from tickets
     where  dateissued > '09/04/2012' and dateissued < dateadd(hour, 0, getdate())
     group by snissued) AS ISSUED ON machines.serialnum = ISSUED.snissued
LEFT JOIN 
    (select 
         snredeemed, cast(sum(amount) as money) / 100 AS [TotalRedeemed] 
     from tickets
     where  dateredeemed > '09/04/2012' and dateredeemed < dateadd(hour, 0, getdate())
     group by snredeemed) AS REDEEMED ON machines.serialnum = REDEEMED.snredeemed

如果可能的话,我想要完成的是第二次和第三次连接是drps.drop1在 where 子句中使用这样的

where  dropdate > drps.dropdate1 and dropdate < drps.dropdate2

但它不起作用

这是工作输出

    序列号 MOCALCSUM MICALCSUM COCALCSUM CICALCSUM TotalIssued TotalRedeemed dropdate1 dropdate2
0-2739-41401 5482 5498 132 148 3258.00 3110.00 2012-09-04 13:36:53.450 2012-09-05 13:55:38.750
0-2459-36182 1110 1054 114 58 1895.00 1657.00 2012-09-04 15:01:19.973 2012-09-05 13:55:38.967

最终结果是我需要总发行日期和总赎回日期范围在每个序列号的最小和最大日期之间

4

1 回答 1

2
declare @datebegin datetime
declare @dateend datetime
set @datebegin = '09/04/2012'
set @dateend = '09/08/2012'

select machines.serialnum,     DRPS.MOCALCSUM,DRPS.MICALCSUM,DRPS.COCALCSUM,DRPS.CICALCSUM,ISSUED.TotalIssued,
        REDEEMED.TotalRedeemed,drps.dropdate1,drps.dropdate2

from (select serialnum
    from machineinfo) as machines


LEFT JOIN (select assetnumber,min(dropdate)as [dropdate1], max(dropdate)as [dropdate2], sum(mocalc) AS [MOCALCSUM],sum(micalc) AS [MICALCSUM],
    sum(cocalc) AS [COCALCSUM],sum(cicalc) AS [CICALCSUM]
    from drops
    where  dropdate > @datebegin and dropdate < @dateend AND  (ignore is null)
            group by assetnumber) AS DRPS
    on machines.serialnum = drps.assetnumber
LEFT JOIN (select snissued,cast(sum(amount)as money)/100 AS [TotalIssued] from tickets
where  (dateissued > (select min(dropdate)
                        from drops 
                        where dropdate > @datebegin AND  (ignore is null) and snissued = assetnumber ))
                        and
       (dateissued < (select max(dropdate)
                        from drops 
                        where dropdate < @dateend AND  (ignore is null) and snissued = assetnumber))
group by snissued) AS ISSUED
ON machines.serialnum=ISSUED.snissued
LEFT JOIN (select snredeemed,cast(sum(amount)as money)/100 AS [TotalRedeemed]

from tickets
where  (dateredeemed > (select min(dropdate)
                        from drops 
                        where dropdate > @datebegin AND  (ignore is null)and snissued = assetnumber))
                        and
       (dateredeemed < (select max(dropdate)
                        from drops 
                        where dropdate < @dateend AND  (ignore is null)and snissued = assetnumber ))

group by snredeemed) AS REDEEMED
ON machines.serialnum=REDEEMED.snredeemed
order by REDEEMED.TotalRedeemed desc

根据评论的建议,我将子查询添加到选择中,现在我得到了预期的结果

于 2012-09-07T20:37:07.850 回答