-
Notifications
You must be signed in to change notification settings - Fork 2
/
Lectures_Module7.sql
135 lines (109 loc) · 3.74 KB
/
Lectures_Module7.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* EdX.org - DAT201x - Module 7 - Lecture Queries */
/* Special, notable notes */
/* - A View (or a Named Query) is a virtual table, sort of like a subquery, that can be preset and present data
as if in a Table.
- Views can also abstract security and permissions with more granularity.
- There are restrictions on INSERT and UPDATE operations on Views. You can only update one of the underlying
Tables at a time when INSERT/UPDATE operations are used on a View. */
/* Demo: Creating Views */
-- Create a View
-- NOTE: Changed nomenclature of View slightly to demark Lecture-driven creations
-- ALSO NOTE: Changed syntax to more formal than given demo queries for consistency
CREATE VIEW SalesLT.vCustomerAddressLecture
AS
SELECT c.CustomerID, FirstName, LastName, AddressLine1, City, StateProvince
FROM SalesLT.Customer AS c
JOIN SalesLT.CustomerAddress AS ca
ON c.CustomerID = ca.CustomerID
JOIN SalesLT.Address AS a
ON ca.AddressID = a.AddressID;
-- Check work:
SELECT CustomerID, City FROM SalesLT.vCustomerAddressLecture;
-- Join the View to a Table
SELECT vc.StateProvince, vc.City, ISNULL(SUM(oh.TotalDue), 0.00) AS Revenue
FROM SalesLT.vCustomerAddressLecture AS vc
LEFT JOIN SalesLT.SalesOrderHeader AS oh
ON oh.CustomerID = vc.CustomerID
GROUP BY vc.StateProvince, vc.City
ORDER BY vc.StateProvince, Revenue DESC;
/* Demo: Temporary Tables and Table Variables */
-- Temporary Table
-- Good for one login session
CREATE TABLE #Colors
(Color varchar(15));
INSERT INTO #Colors
SELECT DISTINCT Color FROM SalesLT.Product;
SELECT * FROM #Colors;
-- Table Variable
-- Good for one batch
-- Highlight this section and exectue all 3 statements at once with Execute
DECLARE @Colors AS TABLE (Color varchar(15));
INSERT INTO @Colors
SELECT DISTINCT Color FROM SalesLT.Product;
SELECT * FROM @Colors;
-- New Batch
SELECT * from #Colors;
SELECT * from @Colors; -- now out of scope
/* Demo: Table Valued Functions (TVFs) */
-- create function
CREATE FUNCTION SalesLT.udfCustomersByCity
(@City AS VARCHAR(20))
RETURNS TABLE
AS
RETURN
(SELECT c.CustomerID, FirstName, LastName, AddressLine1, City, StateProvince
FROM SalesLT.Customer AS c
JOIN SalesLT.CustomerAddress AS ca
ON c.CustomerID = ca.CustomerID
JOIN SalesLT.Address AS a
ON ca.AddressID = a.AddressID
WHERE City = @City);
-- test function
SELECT * FROM SalesLT.udfCustomersByCity('Bellevue');
/* Demo: Derived Tables */
-- Similar to subquery
-- Note limitations given for Derived Tables
SELECT Category, COUNT(ProductID) AS Products
FROM
(SELECT p.ProductID, p.Name AS Product, c.Name as Category
FROM SalesLT.Product AS p
JOIN SalesLT.ProductCategory AS c
ON p.ProductCategoryID = c.ProductCategoryID) AS ProdCats
GROUP BY Category
ORDER BY Category;
/* Demo: Common Table Expression (CTE) */
-- Similar to Derived Tables but easier to read and more flexible
-- Supports multiple calls AND recursion
-- Use OPTION MAXRECURSION to limit runaway logic especially when testing
WITH ProductsByCategory (ProductID, ProductName, Category)
AS
(
SELECT p.ProductID, p.Name, c.Name AS Category
FROM SalesLT.Product AS p
JOIN SalesLT.ProductCategory AS c
ON p.ProductCategoryID = c.ProductCategoryID
)
SELECT Category, COUNT(ProductID) AS Products
FROM ProductsByCategory
GROUP BY Category
ORDER BY Category;
-- Recursive CTE
-- Remind ourselves of the Employee table
SELECT * FROM SalesLT.Employee;
WITH OrgReport (ManagerID, EmployeeID, EmployeeName, Level)
AS
(
-- Anchor query
SELECT e.ManagerID, e.EmployeeID, EmployeeName, 0
FROM SalesLT.Employee AS e
WHERE ManagerID IS NULL
-- Set
UNION ALL
-- Recursive query
SELECT e.ManagerID, e.EmployeeID, e.EmployeeName, Level + 1
FROM SalesLT.Employee AS e
INNER JOIN OrgReport AS o
ON e.ManagerID = o.EmployeeID
)
SELECT * FROM OrgReport
OPTION (MAXRECURSION 3);