1+ use AdventureWorksDW2014;
2+
3+ select OrderDate,sum (SalesAmount)
4+ from FactInternetSales
5+ group by OrderDate
6+ order by OrderDate
7+
8+ -- simple aggregations
9+ -- Use additional aggregations to understand more about product sales such as distribution of sales etc..
10+ SELECT
11+ cat .EnglishProductCategoryName ' Category'
12+ ,sub .EnglishProductSubcategoryName ' SubCategory'
13+ ,count (1 )' Count' -- How many sales where there?
14+ ,sum (s .SalesAmount )' Sales' -- How much sales did we have?
15+ ,avg (s .SalesAmount )' Avg_SalesAmount' -- What was the Avg sale amount?
16+ ,min (s .SalesAmount )' Min_SaleAmount' -- What was the Min sale amount?
17+ ,max (s .SalesAmount )' Max_SaleAmount' -- What was the Max sale amount
18+ FROM FactInternetSales s
19+ LEFT JOIN DimProduct pON s .ProductKey = p .ProductKey
20+ LEFT JOIN DimProductSubcategory subON p .ProductSubcategoryKey = sub .ProductSubcategoryKey
21+ LEFT JOIN DimProductCategory catON sub .ProductCategoryKey = cat .ProductCategoryKey
22+ -- must use group by in order for aggregation to work properly
23+ GROUP BY
24+ cat .EnglishProductCategoryName -- column aliases aren't allowed
25+ ,sub .EnglishProductSubcategoryName
26+ ORDER BY
27+ cat .EnglishProductCategoryName
28+ ,sub .EnglishProductSubcategoryName
29+
30+ -- filter to 2013 with WHERE
31+ SELECT
32+ YEAR(s .OrderDate )' Year'
33+ ,cat .EnglishProductCategoryName ' Category'
34+ ,sub .EnglishProductSubcategoryName ' SubCategory'
35+ ,count (1 )' Count' -- use 1 instead of a field for faster performance
36+ ,sum (s .SalesAmount )' Sales'
37+ ,avg (s .SalesAmount )' Avg_Quantity'
38+ ,min (s .SalesAmount )' Min_SaleAmount'
39+ ,max (s .SalesAmount )' Max_SaleAmount'
40+
41+ FROM FactInternetSales s
42+ INNER JOIN DimProduct pON s .ProductKey = p .ProductKey
43+ INNER JOIN DimProductSubcategory subON p .ProductSubcategoryKey = sub .ProductSubcategoryKey
44+ INNER JOIN DimProductCategory catON sub .ProductCategoryKey = cat .ProductCategoryKey
45+ -- filter
46+ WHERE YEAR(s .OrderDate )= 2013 -- use date function to parse year
47+ -- must use group by in order for aggregation to work properly
48+ GROUP BY
49+ YEAR(s .OrderDate )
50+ ,cat .EnglishProductCategoryName -- column aliases aren't allowed
51+ ,sub .EnglishProductSubcategoryName
52+ ORDER BY
53+ cat .EnglishProductCategoryName
54+ ,sub .EnglishProductSubcategoryName
55+
56+ -- Only show products in 2013 that sold more than $1M USD
57+ SELECT
58+ cat .EnglishProductCategoryName ' Category'
59+ ,sub .EnglishProductSubcategoryName ' SubCategory'
60+ ,count (1 )' Count' -- use 1 instead of a field for faster performance
61+ ,sum (s .SalesAmount )' Sales'
62+ ,avg (s .SalesAmount )' Avg_Quantity'
63+ ,min (s .SalesAmount )' Min_SaleAmount'
64+ ,max (s .SalesAmount )' Max_SaleAmount'
65+ FROM FactInternetSales s
66+ INNER JOIN DimProduct pON s .ProductKey = p .ProductKey
67+ INNER JOIN DimProductSubcategory subON p .ProductSubcategoryKey = sub .ProductSubcategoryKey
68+ INNER JOIN DimProductCategory catON sub .ProductCategoryKey = cat .ProductCategoryKey
69+ -- filter
70+ WHERE YEAR(s .OrderDate )= 2013 -- use date function to parse year
71+ -- must use group by in order for aggregation to work properly
72+ GROUP BY
73+ cat .EnglishProductCategoryName -- column aliases aren't allowed
74+ ,sub .EnglishProductSubcategoryName
75+ -- use HAVING to filter after the aggregate is computed
76+ HAVING
77+ sum (s .SalesAmount )> 1000000
78+ ORDER BY
79+ cat .EnglishProductCategoryName
80+ ,sub .EnglishProductSubcategoryName