Updated on 2024-07-19 GMT+08:00

GROUP BY Conversion

During MySQL/ADB group query, non-group columns can be queried. During GaussDB(DWS) group query, only group columns and aggregate functions can be queried, and if non-group columns are queried, an error will be reported. Therefore, GROUP BY in GaussDB(DWS) is changed to allow querying on non-group columns.

Input

1
SELECT e.department_id, department_name, ROUND(AVG(salary), 0) avg_salary FROM employees e JOIN departments d on e.department_id = d.department_id GROUP BY department_name ORDER BY department_name;

Output

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
SELECT
  e.department_id,
  department_name,
  ROUND (AVG(salary), 0) AS "avg_salary"
FROM
  employees "e"
  JOIN departments "d" ON e.department_id = d.department_id
GROUP BY
  department_name,
  1
ORDER BY
  department_name;