Answer: Aggregate Window Function
Find a detailed explanation of using aggregate window functions in SQL.
We'll cover the following...
Solution
The solution is given below:
Press + to interact
/*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
SELECT
statement selects the columnsMonth
andTotal sales
. TheTotal sales
column retrieves the count of the product in each category using the aggregate window function. We useAS
to set an alias for the columns.Line 4: The data is retrieved from the
Sales
table.Line 5: The
ORDER BY
clause sorts the results by theMonth
column 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 ...
Access this course and 1400+ top-rated courses and projects.