-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart2.sql
More file actions
206 lines (169 loc) · 10.7 KB
/
part2.sql
File metadata and controls
206 lines (169 loc) · 10.7 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
SELECT orderid,
custid,
val,
SUM(val) OVER() AS sumall,
SUM(val) OVER(PARTITION BY custid) AS sumcust
FROM Sales.OrderValues
SELECT orderid,
custid,
val,
CAST(100. * val / SUM(val) OVER() AS NUMERIC(5, 2)) AS pctall,
CAST(100. * val / SUM(val) OVER(PARTITION BY custid) AS NUMERIC(5, 2)) AS pctcust
FROM Sales.OrderValues
SELECT empid,
ordermonth,
qty,
SUM(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW) AS runqty
FROM Sales.EmpOrders
SELECT empid,
ordermonth,
qty,
SUM(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS UNBOUNDED PRECEDING) AS runqty
FROM Sales.EmpOrders
SELECT empid,
ordermonth,
MAX(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS BETWEEN 1 PRECEDING
AND 1 PRECEDING) AS prvqty,
qty AS curqty,
MAX(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS BETWEEN 1 FOLLOWING
AND 1 FOLLOWING) AS nxtqty,
AVG(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS BETWEEN 1 PRECEDING
AND 1 FOLLOWING) AS avgqty
FROM Sales.EmpOrders
--fail
SELECT empid,
ordermonth,
qty,
SUM(qty) OVER(PARTITION BY empid
ORDER BY ordermonth
ROWS BETWEEN INTERVAL '2' MONTH PRECEDING
AND CURRENT ROW) AS sum3month
FROM Sales.EmpOrders
SELECT empid, ordermonth, qty,
(SELECT SUM(qty)
FROM Sales.EmpOrders AS O2
WHERE O2.empid = O1.empid
AND O2.ordermonth BETWEEN DATEADD(month, -2, O1.ordermonth)
AND O1.ordermonth) AS sum3month
FROM Sales.EmpOrders AS O1
SELECT empid, ordermonth, qty,
SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth RANGE BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW) AS runqty
FROM Sales.EmpOrders
SELECT empid, ordermonth, qty,
SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth RANGE UNBOUNDED PRECEDING) AS runqty
FROM Sales.EmpOrders
SELECT empid, ordermonth, qty,
SUM(qty) OVER(PARTITION BY empid ORDER BY ordermonth) AS runqty
FROM Sales.EmpOrders
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cnt
FROM dbo.T1
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cnt
FROM dbo.T1
--EXCLUDE NO OTHERS
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS) AS cnt
FROM dbo.T1
--EXCLUDE CURRENT ROW
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) AS cnt
FROM dbo.T1
--EXCLUDE GROUP
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP) AS cnt
FROM dbo.T1
--EXCLUDE TIES
SELECT keycol, col1,
COUNT(*) OVER(ORDER BY col1 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE TIES) AS cnt
FROM dbo.T1
SELECT empid, ordermonth, qty,
qty - AVG(qty) FILTER (WHERE ordermonth <= DATEADD(month, -3, CURRENT_TIMESTAMP)) OVER(PARTITION BY empid) AS diff
FROM Sales.EmpOrders
SELECT empid, ordermonth, qty,
qty - AVG(CASE WHEN ordermonth <= DATEADD(month, -3, CURRENT_TIMESTAMP) THEN qty END) OVER(PARTITION BY empid) AS diff
FROM Sales.EmpOrders
SELECT orderid, orderdate, empid, custid, val,
val - AVG(val)
FILTER (WHERE custid <> $current_row.custid)
OVER(PARTITION BY empid) AS diff
FROM Sales.OrderValues
SELECT orderid, orderdate, empid, custid, val,
val - AVG(CASE WHEN custid <> $current_row.custid THEN val END)
OVER(PARTITION BY empid) AS diff
FROM Sales.OrderValues
SELECT empid, orderdate, orderid, val,
COUNT(DISTINCT custid) OVER(PARTITION BY empid ORDER BY orderdate) AS numcusts
FROM Sales.OrderValues
SELECT empid, orderdate, orderid, custid, val,
CASE
WHEN ROW_NUMBER() OVER(PARTITION BY empid, custid ORDER BY orderdate) = 1
THEN custid
END AS distict_custid
FROM Sales.OrderValues
WITH C AS
(
SELECT empid, orderdate, orderid, custid, val,
CASE
WHEN ROW_NUMBER() OVER(PARTITION BY empid, custid ORDER BY orderdate) = 1
THEN custid
END AS distinct_custid
FROM Sales.OrderValues
)
SELECT empid, orderdate, orderid, val,
COUNT(distinct_custid) OVER(PARTITION BY empid ORDER BY orderdate) AS numcusts
FROM C;
SELECT empid,
SUM(val) AS emptotal,
SUM(val) / SUM(SUM(val)) OVER() * 100. AS pct
FROM Sales.OrderValues
GROUP BY empid
WITH C AS
(
SELECT empid,
SUM(val) AS emptotal
FROM Sales.OrderValues
GROUP BY empid
)
SELECT empid, emptotal,
emptotal / SUM(emptotal) OVER() * 100. AS pct
FROM C
--fail
WITH C AS
(
SELECT empid, orderdate,
CASE
WHEN ROW_NUMBER() OVER(PARTITION BY empid, custid ORDER BY orderdate) = 1
THEN custid
END AS distinct_custid
FROM Sales.Orders
)
SELECT empid, orderdate,
COUNT(distinct_custid) OVER(PARTITION BY empid ORDER BY orderdate) AS numcusts
FROM C
GROUP BY empid, orderdate;
WITH C AS
(
SELECT empid, orderdate,
CASE
WHEN ROW_NUMBER() OVER(PARTITION BY empid, custid ORDER BY orderdate) = 1
THEN custid
END AS distinct_custid
FROM Sales.Orders
)
SELECT empid, orderdate,
SUM(COUNT(distinct_custid)) OVER(PARTITION BY empid ORDER BY orderdate) AS numcusts
FROM C
GROUP BY empid, orderdate;