SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 compare today and yesterday time range on sales
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

usafelix
Yak Posting Veteran

Hong Kong
90 Posts

Posted - 08/23/2014 :  07:53:09  Show Profile  Reply with Quote
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
Constraint Violating Yak Guru

397 Posts

Posted - 08/23/2014 :  12:22:09  Show Profile  Reply with Quote
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
Yak Posting Veteran

Hong Kong
90 Posts

Posted - 08/23/2014 :  13:32:01  Show Profile  Reply with Quote
Thanks so much !!!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000