在Having子句中按功能分组,子查询和嵌套分组

时间:2014-10-06 11:45:12

标签: sql oracle nested having clause

尝试显示订单数量最多的工作人员的详细信息,在oracle中有一个错误,指出群组功能嵌套太深'。

SELECT c.Staff_No, s.First_name, s.Last_Name, max(COUNT(*)) AS "Number Of Orders"
  FROM Cust_Order c, 
       Staff s   
 WHERE c.Staff_No = s.Staff_No
 GROUP 
    BY c.Staff_No, s.First_name, s.Last_Name
HAVING MAX(COUNT(*)) > (SELECT MAX((COUNT(*))
                          FROM Cust_Order c, Staff s 
                         WHERE c.Staff_No = s.Staff_No);           

1 个答案:

答案 0 :(得分:1)

相反,使用分析函数:

SELECT Staff_No, First_name, Last_Name, "Number Of Orders"
FROM (SELECT c.Staff_No, s.First_name, s.Last_Name, COUNT(*) AS "Number Of Orders",
             max(count(*)) over () as maxcount
      FROM Cust_Order c JOIN
           Staff s                                                                                                                                                                                                                                                                                                                          
           ON c.Staff_No = s.Staff_No
      GROUP BY c.Staff_No, s.First_name, s.Last_Name
     ) cs
WHERE "Number Of Orders" = maxcount;

这也应该有更好的表现。