select f1,f2,sum(f3) from tablename group by f1,f2
不能
select f1,f2,sum(f3) from tablename group by f1
不能
select f1,f2,sum(f3) from tablename group by f2
不能
select f1,f2,sum(f3) from tablename group by f1,f2,f3
不能
select f1,sum(f3) from tablename group by f1,f2
不能
select f2,sum(f3) from tablename group by f1,f2
使用 GROUP BY 分组多行
GROUP BY 子句用来为结果集中的每一行产生聚合值。如果聚合函数没有使用 GROUP BY 子句,则只为 SELECT 语句报告一个聚合值。
以下示例返回分类 2 中每种产品已销售的单位数量:
USE Northwind
SELECT OrdD.ProductID AS ProdID,
SUM(OrdD.Quantity) AS AmountSold
FROM [Order Details] AS OrdD JOIN Products as Prd
ON OrdD.ProductID = Prd.ProductID
AND Prd.CategoryID = 2
GROUP BY OrdD.ProductID