Answer: Creating Views with Aggregate Functions
Find a detailed explanation of creating views with aggregate functions in SQL.
We'll cover the following...
We'll cover the following...
Solution
The solution is given below:
MySQL
/* The query to create a view named MonthlyCategorySalesPerformanceto monitor performance per month of each category */CREATE VIEW MonthlyCategorySalesPerformance ASSELECT S.Month AS 'Month',PC.CategoryName AS 'Category Name',SUM(S.SalesAmount) AS 'Total Sales Amount'FROM Sales SJOIN ProductCategories PC ON S.CategoryID = PC.CategoryIDGROUP BY S.Month, PC.CategoryName;SELECT * FROM MonthlyCategorySalesPerformance;
Code explanation
The explanation of the solution code is given below:
Line 3: The
CREATE VIEWstatement creates a view namedMonthlyCategorySalesPerformance.Lines 4–6: The columns of the view are
Month,Category Name, and the calculated columnTotal Sales Amount.Line 7: The data is retrieved from the
Salestable.Line 8: The
JOINis performed withSalesandProductCategorieson the columnsCategoryIDin both the tables.Line 9: The result of the
JOINis grouped byMonthandCategoryName.Line 11: The
SELECTstatement displays the content of the created view.
Recalling relevant concepts
We have covered the following concepts in this question: ...