Answer: Aggregate Window Function
Find a detailed explanation of using aggregate window functions in SQL.
We'll cover the following...
We'll cover the following...
Solution
The solution is given below:
MySQL
Saved
/*Query to find the count of sales for each month*/SELECT DISTINCT Month,COUNT(*) OVER (PARTITION BY Month) AS 'Total Sales'FROM SalesORDER BY FIELD(Month, "January", "February", "March", "April", "May");
Code explanation
The explanation of the solution code is given below:
Lines 2–3: The
SELECTstatement selects the columnsMonthandTotal sales. TheTotal salescolumn retrieves the count of the product in each category using the aggregate window function. We useASto set an alias for the columns.Line 4: The data is retrieved from the
Salestable.Line 5: The
ORDER BYclause sorts the results by theMonthcolumn in custom order of months using theFIELD()function.
Recalling relevant concepts
We have covered the following concepts in this question:
Selective columns
Aliases
Aggregate functions
Sorting the results ...