简体   繁体   中英

Not a single-group group function Oracle SQL

I have the following table: 在此处输入图片说明

I have to find the name and the country of the department with the highest number of employees.

SELECT d.department_name, c.country_name FROM employees e, departments d, locations l, countries c
WHERE d.location_id = l.location_id AND l.country_id = c.country_id
HAVING MAX(e.employee_id) = (SELECT MAX(MAX(employee_id)) FROM employees GROUP BY department_id);

I'm getting a not a single-group group function error. Why is that?

If am not wrong this is what you are looking for

SELECT * 
FROM   (SELECT d.department_name, 
               c.country_name 
        FROM   employees e 
               INNER JOIN departments d 
                       ON e.department_id = d.department_id 
               INNER JOIN locations l 
                       ON d.location_id = l.location_id 
               INNER JOIN countries c 
                       ON l.country_id = c.country_id 
        GROUP  BY d.department_name, 
                  c.country_name 
        ORDER  BY Count(1) DESC) 
WHERE  ROWNUM = 1 

is this what you want?

SELECT d.department_name, c.country_name FROM employees e, departments d, locations l, countries c
WHERE d.location_id = l.location_id AND l.country_id = c.country_id
AND ROWNUM = 1
ORDER BY e.employee_id DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM