Okay guys here is the question. I have to list the department ID, department name, count of sales reps, and average commission rate for each department. Also I need to Group by department, and sort by average commission rate in ascending order.
I am receiving an error at line 1 under the De.Dept_ID saying that is not a group by expression
Here is my code:
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;
I appreciate the input
SELECT DE.Dept_ID as Dept_ID,
Dept_Name,
COUNT(SR.Sales_Rep_ID) as NumOfSalesR,
AVG(Comm_Rate) as AVGCOM
FROM DEPT_arb DE, SALES_REP_arb SR, COMMISSION_arb C
WHERE DE.Dept_ID = SR.Dept_ID
GROUP BY E.Dept_ID,
Dept_Name,
ORDER BY C.Comm_Rate;
You may also consider properly joining the tables, for ie:
SELECT DE.Dept_ID as Dept_ID,
Dept_Name,
COUNT(SR.Sales_Rep_ID) as NumOfSalesR,
AVG(Comm_Rate) as AVGCOM
FROM DEPT_arb DE
JOIN SALES_REP_arb SR on DE.Dept_ID = SR.Dept_Id
JOIN COMMISION_arb C on SR.Comm_Class = C.Comm_Class
GROUP BY E.Dept_ID,
Dept_Name,
ORDER BY C.Comm_Rate;
Add a group by.
SELECT DE.Dept_ID as Dept_ID, Dept_Name,
COUNT(SR.Sales_Rep_ID) as NumOfSalesR,
Comm_Rate as AVGCOM
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, Comm_Rate
rewrite this way and see if it gives you what you want...
SELECT d.Dept_ID, d.Dept_Name,
Count(r.Sales_Rep_ID) NumOfSalesR,
c.Comm_Rate AVGCOM
FROM DEPT_arb d,
Join SALES_REP_arb r
On r.Dept_ID = d.Dept_ID
Join COMMISSION_arb c
On c.Comm_Class = r.Comm_Class
Group By d.Dept_ID, d.Dept_Name, c.Comm_Rate
Where c.Comm_Rate =
(Select AVG(Comm_Rate)
From COMMISSION_arb
Where Comm_Class = r.Comm_Class)
Order By c.Comm_Rate;
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.