SQL 查询多个总和和组
SQL query Multiple Sums and Groups
我有以下 Table 有多个不同日期的多道菜
[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 查询,它将组合每个 unique/distinct Dish_Id 并将其等效数量和总计相加,但根据日期值分组而不影响其每件商品的价格
因此,如果您 运行 上面 table 的查询将产生以下结果集:
[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 是正确的方法,但我不确定如何对多列求和()并仅将 groupby 用于 "Date" 值而不影响 "Price"
可能是这样的
select Date, Dish_Id, sum(quantity), Price, Sum(Total)
from Dishes
group by Date, Price
编辑************
现在开始工作了
谢谢大家
这个怎么样?
SELECT DATE
,dish_id
,sum(quantity) AS Quantity_Total
,sum(price * quantity) AS TotalPrice
FROM table1
GROUP BY DATE
,dish_id
在线看这里example/sandboxhttp://rextester.com/OLAJ52246
我有以下 Table 有多个不同日期的多道菜
[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 查询,它将组合每个 unique/distinct Dish_Id 并将其等效数量和总计相加,但根据日期值分组而不影响其每件商品的价格 因此,如果您 运行 上面 table 的查询将产生以下结果集:
[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 是正确的方法,但我不确定如何对多列求和()并仅将 groupby 用于 "Date" 值而不影响 "Price" 可能是这样的
select Date, Dish_Id, sum(quantity), Price, Sum(Total)
from Dishes
group by Date, Price
编辑************ 现在开始工作了 谢谢大家
这个怎么样?
SELECT DATE
,dish_id
,sum(quantity) AS Quantity_Total
,sum(price * quantity) AS TotalPrice
FROM table1
GROUP BY DATE
,dish_id
在线看这里example/sandboxhttp://rextester.com/OLAJ52246