SQLTeam.com | Weblogs | Forums

Previous Month and current month Using sql server query

sql2012
#1

I have one sales table Order, This have previous month sales and this month sales , so I need 3 columns i.e. Name, this month total sales for selected name in where query and previous month sales.

Need in single query from same table

#2

Something like this perhaps:

select customername
      ,sum(case
              when salesdate>eomonth(current_timestamp,-1)
              then 0
              else salesprice
           end
          )
       as lastmonth
      ,sum(case
              when salesdate>eomonth(current_timestamp,-1)
              then salesprice
              else 0
           end
          )
       as thismonth
  from order
 where salesdate>=dateadd(day,1,eomonth(current_timestamp,-2))
   and salesdate<dateadd(day,1,eomonth(current_timestamp))
 group by customername
;