- SELECT关键词后面出现的字段必须同时出现在GROUP BY后方。
SELECT col_a,col_b FROM table_a GROUP BY col_a,col_b;
SELECT col_a,col_c FROM table_a GROUP BY col_a,col_b;
- SELECT 语句后方可以跟聚合函数,而聚合函数字段可以不出现在GROUP BY后方。
SELECT col_a,sum(col_b) FROM table_a GROUP BY col_a;
- SELECT语句后方不一定要跟GROUP BY后方的所有字段,可以是其子集。
SELECT col_a FROM table_a GROUP BY col_a,col_b;
- 如果有ORDER BY字段,也要遵循和SELECT一样的规则,否则会出现ORA-00979报错。
SELECT col_a,col_b FROM table_a GROUP BY col_a,col_b ORDER BY col_a,col_b;
SELECT col_a,col_b FROM table_a GROUP BY col_a,col_b ORDER BY col_a,col_c;