-
Notifications
You must be signed in to change notification settings - Fork 0
/
Introduction to Aggregation.sql
81 lines (58 loc) · 2.59 KB
/
Introduction to Aggregation.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
--Introduction to Aggregation
--Minoo Modares
USE Northwind
-- here's a simple SELECT statement from the Products table
SELECT ProductID,
productname,
unitprice,
UnitsInStock
FROM Products
ORDER BY ProductID
-- with no where clause, we get a record here for every product in the products table
-- but what if your manager asks you to display a *summary* of the products?
-- this summarization is known as "aggregating" or "aggregation". -- we can use aggregating functions for that.
-- in SQL, there are built-in aggregating functions
--AVG()
--MIN()
--MAX()
--COUNT()
--SUM()
--STDEV()
--VAR()
--note that Except for COUNT(*), aggregate functions ignore null values.
--For example:
SELECT MAX(unitprice) AS [maximum unit price]
FROM Products
-- in this case, we have summarized all the prices of all products
-- into a single number - the max
-- we aggregate (summarize) all the product records into a single group,-- then we can ask questions about the group
-- what else can we find out about the group?
SELECT AVG(unitprice) AS [average unit price],
MIN(ReorderLevel) AS [minimum reorder level],
MIN(unitprice) AS [minimum unit price],
MAX(unitprice) AS [maximum unitprice]
From Products
SELECT STDEV(unitprice) AS [standard deviation of unit price],
VAR(unitprice) AS [variance of unit price],
COUNT(productID) AS [number of products],
SUM(unitsinStock) AS [total number of units in stock]
From Products
-- we can also include expressions in an aggregate function:
--for example, your manager is asking you about the total value of orders you have received
SELECT SUM(unitprice * unitsonorder) AS [total value of orders]
FROM Products
-- in the above examples we were aggregating over the whole table
-- by adding a WHERE clause, you can limit the aggregation
-- for example, the MAX is calculated over unit prices of products in the stated categories
SELECT MAX(unitprice) AS [maximum unit price]
FROM Products
WHERE CategoryID IN (1, 2, 3)
-- think of the above example as happening in this order
-- 1 - the WHERE clause - records are eliminated first based on the specified condition
-- 2 - the max - aggregate calculated over remaining records
--Another example:
--The average of uniteprices for products that are not discontinued and the unit price is more than $100
SELECT AVG(unitprice) AS [average unit price]
From Products
WHERE Discontinued=0
AND unitprice > 100