GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。
合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句。
语法
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
例子1:查找每个客户的总金额
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer;
例子2:GROUP BY 一个以上的列
SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate