前几天得到了一些帮助,现在真的想做出更好的改变
这是代码
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
如果可能的话,我想要完成的第二个和第三个连接是在像这样的where子句中使用drps.drop1
where dropdate > drps.dropdate1 and dropdate < drps.dropdate2
但它不起作用
这是工作输出
serialnum 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
最终结果是我需要总发布和总兑换日期范围介于每个序列号的最小和最大日期之间
答案 0 :(得分: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
基于评论的建议我将子查询添加到选择中,我现在得到预期的结果