Group by的用法
GROUP BY 语句根据一个或多个列对结果集进行分组。在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。
Group by用法:
SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
分组后的条件使用 HAVING 来限定,WHERE 是对原始数据进行条件限制。几个关键字的使用顺序为 where 、group by 、having、order by ,
单个字段场景:
SELECT
NAME,
SUM (*)
FROM
employee_tbl
WHERE
ID <> 1
GROUP BY
NAME
HAVING
SUM (*) > 5
ORDER BY
SUM (*) DESC;
单个字段场景:
SELECT
CONTENTID,
LOGINNAME,
"MAX" (CREATETIME) AS CREATETIME
FROM
USER_SCORE_RECORD
WHERE
CONTENTID = 640658720
GROUP BY
CONTENTID,
LOGINNAME