Suppose I have a table with the following columns:
- TYPE
- DEPARTMENT
- SUPPLIER
- ORDER
- TOTAL
And I wanted to query this data so that I get ordered results, first grouped by TYPE. The order being the Number of Orders. Then the following Query Works Well for me (http://sqlfiddle.com/#!15/78cc1/1):
WITH company_sales(type, department, supplier, order_number, total) AS (
VALUES
('Edibles' , 'Department-1', 'Supplier-1' , 'ORDER-1' , 10)
, ('Edibles' , 'Department-1', 'Supplier-2' , 'ORDER-2' , 20)
, ('Edibles' , 'Department-1', 'Supplier-3' , 'ORDER-3' , 30)
, ('Edibles' , 'Department-1', 'Supplier-4' , 'ORDER-4' , 40)
, ('Edibles' , 'Department-2', 'Supplier-5' , 'ORDER-5' , 50)
, ('Edibles' , 'Department-2', 'Supplier-6' , 'ORDER-6' , 60)
, ('Edibles' , 'Department-3', 'Supplier-7' , 'ORDER-7' , 70)
, ('Edibles' , 'Department-3', 'Supplier-8' , 'ORDER-8' , 80)
, ('Edibles' , 'Department-3', 'Supplier-9' , 'ORDER-9' , 90)
, ('Edibles' , 'Department-3', 'Supplier-9' , 'ORDER-10', 100)
, ('Edibles' , 'Department-4', 'Supplier-10', 'ORDER-11', 110)
, ('Non-Edibles', 'Department-2', 'Supplier-11', 'ORDER-12', 1000)
, ('Non-Edibles', 'Department-3', 'Supplier-12', 'ORDER-13', 1010)
, ('Non-Edibles', 'Department-3', 'Supplier-13', 'ORDER-14', 1020)
, ('Non-Edibles', 'Department-3', 'Supplier-14', 'ORDER-15', 1030)
, ('Non-Edibles', 'Department-3', 'Supplier-14', 'ORDER-16', 1040)
, ('Non-Edibles', 'Department-4', 'Supplier-15', 'ORDER-17', 1050)
)
SELECT cs.type,
count(*) sum_total_count,
sum(total) sum_grand_total
FROM company_sales cs
GROUP BY cs.type
ORDER BY Sum(Count(*)) OVER (partition BY cs.type) DESC,
cs.type ASC;
And if I wanted to query this data so that I get ordered results, first grouped by TYPE and then by DEPARTMENT. The order being the Number of Orders. Then the following Query Works Well for me (http://sqlfiddle.com/#!15/78cc1/2):
WITH company_sales(type, department, supplier, order_number, total) AS ( ...)
SELECT cs.type,
cs.department,
count(*) sum_total_count,
sum(total) sum_grand_total
FROM company_sales cs
GROUP BY cs.type,
cs.department
ORDER BY Sum(Count(*)) OVER (partition BY cs.type) DESC,
Sum(Count(*)) OVER (partition BY cs.type, cs.department) DESC,
cs.type ASC,
cs.department ASC;
However, following the same pattern when I want ordered results, first grouped by TYPE and then by DEPARTMENT, and then by SUPPLIER, with the order being the Number of Orders. Then the following Query DOES NOT WORK for me (http://sqlfiddle.com/#!15/78cc1/3):
WITH company_sales(type, department, supplier, order_number, total) AS (...)
SELECT cs.type,
cs.department,
cs.supplier,
count(*) sum_total_count,
sum(total) sum_grand_total
FROM company_sales cs
GROUP BY cs.type,
cs.department,
cs.supplier
ORDER BY Sum(Count(*)) OVER (partition BY cs.type) DESC,
Sum(Count(*)) OVER (partition BY cs.type, cs.department) DESC,
Sum(Count(*)) OVER (partition BY cs.type, cs.department, cs.supplier) DESC,
cs.type ASC,
cs.department ASC,
cs.supplier ASC;
The above query results in the following:
Whereas, I desire the following:
Where am I going wrong?