Oracle 12c - 如何通过此查询进行GROUP和ORDER BY

时间:2014-08-16 16:31:47

标签: sql oracle

以下查询抱怨GROUP BY和ORDER BY表达式 - 请帮我纠正 - 谢谢 SELECT a.FirstName|| ' ' ||a.LastName AS "Name", ROUND(SUM((b.UnitPrice - b.Discount) * b.Quantity)/100000) AS "Sales" FROM Employees a, Order_Details b, Orders c WHERE a.EmployeeID = c.EmployeeID AND b.OrderID = c.OrderID GROUP BY 1 ORDER BY 2 DESC FETCH FIRST 5 ROWS ONLY;

这是错误:

a.FirstName|| ' ' ||a.LastName AS "Name",
ERROR at line 2:
ORA-00979: not a GROUP BY expression`

此致 汤妮雅

2 个答案:

答案 0 :(得分:3)

尝试:

SELECT    a.FirstName|| ' ' ||a.LastName AS "Name",    
    ROUND(SUM((b.UnitPrice - b.Discount) * b.Quantity)/100000) AS
    "Sales"    
FROM     
    Employees a, Order_Details b, Orders c  
WHERE    
    a.EmployeeID = c.EmployeeID AND     
    b.OrderID = c.OrderID  
GROUP BY
    a.FirstName, a.LastName 
ORDER BY 
    2 DESC  
FETCH FIRST 5 ROWS ONLY;

答案 1 :(得分:0)

SELECT a.FirstName || ' ' || a.LastName AS "Name", 
       ROUND(SUM((b.UnitPrice - b.Discount) * b.Quantity)/100000) AS "Sales" 
  FROM Employees a, Order_Details b, Orders c 
 WHERE a.EmployeeID = c.EmployeeID
   AND b.OrderID = c.OrderID 
GROUP BY a.FirstName || ' ' || a.LastName
ORDER BY 2 DESC 
FETCH FIRST 5 ROWS ONLY;

ORDER BY子句不同,您无法使用AS列名称或列位置编号。如果分组列是表达式,则必须在GROUP BY子句中使用该表达式,即a.FirstName || ' ' || a.LastNameSUM是将应用于此组的聚合函数,即使它是像这样的混乱表达式。