Suppose I have a table with 3 columns:
- YEAR
- COMPANY
- DEPARTMENT
- SALES
And I wanted to display the following:
- COMPANY
- DEPARTMENT
- SUM(SALES)
The additional requirement being that the COMPANY with the Maximum Sales (across all departments) shows first, and the rest of the results ordered accordingly.
How would I go about writing my query?
I've tried the following, and it doesn't work:
SELECT t1.company,
cs1.deparment,
SUM(cs1.sales)
FROM company_sales cs1,
(SELECT cs2.company,
SUM(cs2.sales)
FROM company_sales cs2
WHERE cs2.company IS NOT NULL
GROUP BY cs2.company
ORDER BY 2 DESC) t1
WHERE cs1.company = t1.company
GROUP BY t1.company,
cs1.deparment;