Reputation:
SELECT p.pdept.dno,
MAX(SUM(p.budget)) AS max
FROM Proj111 p
GROUP BY p.pdept.dno
Although I have included all the necessary attributes in select statements to group by clause it will generate above issue? How to solve it?
Upvotes: 2
Views: 497
Reputation: 521178
You can't nest aggregate functions like that. If you want to get the maximum sum from each group, then you can try this:
WITH cte AS (
SELECT p.pdept.dno AS dept,
SUM(p.budget) AS budget
FROM Proj111 p
GROUP BY p.pdept.dno
)
SELECT t.dept,
t.budget
FROM cte t
WHERE t.budget = (SELECT MAX(budget) FROM cte)
The common table expression which I have named cte
finds the budgets for each department. The query then restricts this result to the department with the maximum budget by again querying the cte
for the maximum budget.
Upvotes: 5