以下查询抱怨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`
此致 汤妮雅
答案 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.LastName
。 SUM
是将应用于此组的聚合函数,即使它是像这样的混乱表达式。