SQLTeam.com | Weblogs | Forums

Previous Month and current month Using sql server query


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


Something like this perhaps:

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