0

Hello the below query gives the following output :

Sales     RestaurantID    Date
100.00      1             2016-01-01 
200.00      1             2016-01-02 
300.00      1             2016-01-03 

Instead of that i need to convert the Date values to columns and show total sales under each day so the output should be like this

RestaurantID    2016-01-01  2016-01-02  2016-01-03     
1               100.00      200.00      300.00   

Query

select sum(Sales),RestaurantID, dateadd(DAY,0, datediff(day,0, Date)) as Date
from DailySales
group by dateadd(DAY,0, datediff(day,0, Date)),RestaurantID
Ayman
  • 99
  • 1
  • 3
  • 12
  • 2
    Possible duplicate of [Convert Rows to columns using 'Pivot' in SQL Server](http://stackoverflow.com/questions/15931607/convert-rows-to-columns-using-pivot-in-sql-server) – Gurwinder Singh Dec 27 '16 at 11:52

2 Answers2

0
declare @pivcol varchar(max)
SELECT @pivcol = COALESCE(@pivcol + ',','') + '[' + date + ']'         
FROM  DailySales

exec('select * from DailySales 
pivot (sum(sales) for date in ('+ @pivcol +'))')                        
karan yadav
  • 59
  • 1
  • 6
0

try this

 SELECT *
FROM Table_3
PIVOT(SUM(Sales) 
FOR Date IN ([2016-01-01],[2016-01-02],[2016-01-03])) AS PVTTable

OUTPUT:

enter image description here

Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30