Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 compare today and yesterday time range on sales

Author  Topic 

usafelix
Posting Yak Master

165 Posts

Posted - 2014-08-23 : 07:53:09
boss request me to do a report for each hour compare yesterday each hour of sales amount their output is below , pls give idea how to write a query.

disire output 08:00 am -23:00pm
shop today Time yesterday current Amt Yest Amt diff amount
001 13:00-14:00 13:00-14:00 $10000 $20000 -10000 (down)
002 14:00-15:00 14:00-15:00 $10000 $15000 500 (up)

Time as at HH:MM
Yesterday Total $20000
today Total $35000

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-08-23 : 12:22:09
Try this:
select shop
,ltrim(str(datepart(hh,yourdatetimefield)))+':00 - '+ltrim(str(datepart(hh,yourdatetimefield)))+':59' as time_span
,sum(case
when datediff(dd,yourdatetimefield,getdate())=0
then amt
else 0
end
) as current_amount
,sum(case
when datediff(dd,yourdatetimefield,getdate())=1
then amt
else 0
end
) as yesterday_amount
,sum(amt*case
when datediff(dd,yourdatetimefield,getdate())=1
then -1
else 1
end
) as diff_amount
,case sign(sum(amt*case
when datediff(dd,yourdatetimefield,getdate())=1
then -1
else 1
end
)
)
when 0 then 'Same'
when 1 then 'Up'
else 'Down'
end as amount_direction
from yourtable
where yourdatetimefield>=cast(dateadd(dd,-1,getdate()) as date)
and yourdatetimefield<cast(dateadd(dd,1,getdate()) as date)
group by shop
,datepart(hh,yourdatetimefield)
Go to Top of Page

usafelix
Posting Yak Master

165 Posts

Posted - 2014-08-23 : 13:32:01
Thanks so much !!!
Go to Top of Page
   

- Advertisement -