我的下表有多个不同日期的菜肴
[Dishes]
Date | Dish_Id | Quantity | Price | Total
============================================================================
2016-09-09 00:00:00.000 5 1 50.00 50.00
2016-09-09 00:00:00.000 3 1 30.00 30.00
2016-09-10 00:00:00.000 10 1 100.00 100.00
2016-09-09 00:00:00.000 5 1 50.00 50.00
2016-09-09 00:00:00.000 7 1 70.00 70.00
2016-09-09 00:00:00.000 7 1 70.00 70.00
2016-09-09 00:00:00.000 3 1 30.00 30.00
2016-09-10 00:00:00.000 3 1 30.00 30.00
我想要做的是一个SQL查询,它将结合每个唯一/不同的Dish_Id并将其等效的数量和总和相加,但是按照日期值分组而不影响其每个项目的价格 因此,如果您在上面的表上运行查询,它将生成以下结果集:
[Result]
Date | Dish_Id | Quantity | Price | Total
============================================================================
2016-09-09 00:00:00.000 5 2 50.00 100.00
2016-09-09 00:00:00.000 3 2 30.00 60.00
2016-09-09 00:00:00.000 7 2 70.00 140.00
2016-09-10 00:00:00.000 10 1 100.00 100.00
2016-09-10 00:00:00.000 3 1 30.00 30.00
我确定GroupBy是正确的方法,但我不确定如何sum()多列,只是使用groupby为"日期"价值不影响"价格" 这样的事可能
select Date, Dish_Id, sum(quantity), Price, Sum(Total)
from Dishes
group by Date, Price
EDIT ************ 现在就开始工作了 感谢每个人
答案 0 :(得分:1)
这个怎么样?
SELECT DATE
,dish_id
,sum(quantity) AS Quantity_Total
,sum(price * quantity) AS TotalPrice
FROM table1
GROUP BY DATE
,dish_id
在此处查看在线示例/沙箱http://rextester.com/OLAJ52246