-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch05.sql
119 lines (105 loc) · 2.73 KB
/
ch05.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
-- Exercise 1
SELECT orderid, orderdate, custid, empid,
DATEFROMPARTS(YEAR(orderdate), 12, 31) AS endofyear
FROM Sales.Orders
WHERE orderdate <> DATEFROMPARTS(YEAR(orderdate), 12, 31);
SELECT *
FROM
(
SELECT orderid, orderdate, custid, empid,
DATEFROMPARTS(YEAR(orderdate), 12, 31) AS endofyear
FROM Sales.Orders
) AS T
WHERE orderdate <> endofyear;
WITH C AS
(
SELECT *,
DATEFROMPARTS(YEAR(orderdate), 12, 31) AS endofyear
FROM Sales.Orders
)
SELECT orderid, orderdate, custid, empid, endofyear
FROM C
WHERE orderdate <> endofyear;
-- Exercise 2-1
SELECT empid, MAX(orderdate) AS maxorderdate
FROM Sales.Orders
GROUP BY empid;
-- Exercise 2-2
SELECT Sales.Orders.empid, orderdate, orderid, custid
FROM Sales.Orders
INNER JOIN
(
SELECT empid, MAX(orderdate) AS maxorderdate
FROM Sales.Orders
GROUP BY empid
) AS T
ON Sales.Orders.empid = T.empid
AND Sales.Orders.orderdate = T.maxorderdate;
-- Exercise 3-1
SELECT orderid, orderdate, custid, empid,
ROW_NUMBER() OVER (ORDER BY orderdate, orderid) AS rownum
FROM Sales.Orders;
-- Exercise 3-2
WITH order_rownum AS
(
SELECT orderid, orderdate, custid, empid, ROW_NUMBER() OVER (ORDER BY orderdate, orderid) AS rownum
FROM Sales.Orders
)
SELECT *
FROM order_rownum
WHERE rownum BETWEEN 11 AND 20;
SELECT orderid, orderdate, custid, empid, ROW_NUMBER() OVER (ORDER BY orderdate, orderid) AS rownum
FROM Sales.Orders
ORDER BY rownum
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;
-- Exercise 4
WITH employee_manager AS
(
SELECT empid, mgrid, firstname, lastname
FROM HR.Employees
WHERE empid = 9
UNION ALL
SELECT HR.Employees.empid, HR.Employees.mgrid, HR.Employees.firstname, HR.Employees.lastname
FROM employee_manager
INNER JOIN HR.Employees ON employee_manager.mgrid = HR.Employees.empid
)
SELECT *
FROM employee_manager
-- Exercise 5-1
DROP VIEW IF EXISTS Sales.VEmpOrders;
GO
CREATE VIEW Sales.VEmpOrders
AS
SELECT empid, YEAR(orderdate) AS orderyear, SUM(qty) AS qty
FROM Sales.Orders
INNER JOIN Sales.OrderDetails
ON Sales.Orders.orderid = Sales.OrderDetails.orderid
GROUP BY empid, YEAR(orderdate);
GO
SELECT *
FROM Sales.VEmpOrders
ORDER BY empid, orderyear;
-- Exercise 5-2
SELECT *, SUM(qty) OVER (PARTITION BY empid ORDER BY orderyear) AS runqty
FROM Sales.VEmpOrders;
-- Exercise 6-1
DROP FUNCTION IF EXISTS Production.TopProducts;
GO
CREATE FUNCTION Production.TopProducts
(
@supid AS INT,
@n AS INT
)
RETURNS TABLE
RETURN
SELECT productid, productname, unitprice
FROM Production.Products
WHERE supplierid = @supid
ORDER BY unitprice DESC
OFFSET 0 ROW FETCH NEXT @n ROWS ONLY;
GO
SELECT * FROM Production.TopProducts(5, 2);
-- Exercise 6-2
SELECT S.supplierid, S.companyname, TP.*
FROM Production.Suppliers AS S
CROSS APPLY Production.TopProducts(S.supplierid, 2) AS TP