Reputation: 152
Got some help the other day on this and now really want to make a change for the better
Here is code
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
what I would like to accomplish if possible is for the second and third join is to use the drps.drop1
in the where clause like this
where dropdate > drps.dropdate1 and dropdate < drps.dropdate2
but it does not work
here is working output
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
end result is I need total issued and total redeemed date range to be between the min and max date per serial number
Upvotes: 1
Views: 1184
Reputation: 152
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
based on the suggestion from comments i added the subquery into the selects and i am getting expected results now
Upvotes: 2