Sql查询按日获得销售额

时间:2016-12-27 11:50:54

标签: sql sql-server

您好以下查询提供以下输出:

TFS

而不是我需要将Date值转换为列并显示每天的总销售额,因此输出应该是这样的

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

查询

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

2 个答案:

答案 0 :(得分:0)

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

exec('select * from DailySales 
pivot (sum(sales) for date in ('+ @pivcol +'))')                        

答案 1 :(得分:0)

试试这个

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

<强>输出:

enter image description here