好的家伙在这里是个问题。 我必须列出每个部门的部门ID,部门名称,销售代表数量和平均佣金率。此外,我需要按部门分组,并按平均佣金率按升序排序。
我在De.Dept_ID下的第1行收到错误,说不是按表达式分组
这是我的代码:
SELECT DE.Dept_ID as DeptID, Dept_Name as DeptName, COUNT(SR.Sales_Rep_ID) as SalesRepCount,
Comm_Rate as AvgCommRate
FROM DEPT_arb DE, SALES_REP_arb SR, COMMISSION_arb C
WHERE DE.Dept_ID = SR.Dept_ID AND
Comm_Rate = (SELECT AVG(Comm_Rate) FROM COMMISSION_arb WHERE SR.Comm_Class = C.Comm_Class)
GROUP BY Dept_Name
ORDER BY C.Comm_Rate;
我很欣赏输入
答案 0 :(得分:0)
您只能按Dept_Name
分组,但您也选择Dept_ID
,因此您也必须将其添加到GROUP BY
子句中。
SELECT DE.Dept_ID as DeptID, Dept_Name as DeptName, COUNT(SR.Sales_Rep_ID) as SalesRepCount,
Comm_Rate as AvgCommRate
FROM DEPT_arb DE, SALES_REP_arb SR, COMMISSION_arb C
WHERE DE.Dept_ID = SR.Dept_ID AND
Comm_Rate = (SELECT AVG(Comm_Rate) FROM COMMISSION_arb WHERE SR.Comm_Class = C.Comm_Class)
GROUP BY DE.Dept_ID, Dept_Name
ORDER BY C.Comm_Rate;
MySQL允许省略它,但我认为它是唯一可以做到的数据库。