GROUP BY [ALL] (column1[,column2,...]) 分组
在select语句中所定义的任何列,如果没有被应用于聚合中,则必须以在select语句中的顺序,包含到GROUP BY语句中。
GROUP BY ALL: 如果在select语句中使用了where语句,则被筛选的分组仍会在结果中返回一行,而不是对列的聚合,一个NULL值会被返回
SELECT shareId, MIN(price) minPrice, MAX(price) maxPrice
FROM sharePrices
WHERE shareId < 200
GROUP BY shareId
HAVING 分组后筛选
用在GROUP BY [ALL]后,对分组后的数据进行筛选,此时GROUP BY ALL的结果也会被筛选。
SELECT custId, COUNT(*)
FROM custBankTrans
WHERE transDate BETWEEN 'startDate' AND 'endDate'
GROUP BY custId
HAVING COUNT(*) > 10
DISTINCT 独特值,去重
去除相同的记录结果,保证记录都是唯一的
SELECT DISTINCT s.shareDesc, sp.price, sp.priceDate
FROM shares s
JOIN sharePrices sp
ON sp.shareId = s.shareId