-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL Advanced 32-43.sql
More file actions
216 lines (187 loc) · 6.29 KB
/
SQL Advanced 32-43.sql
File metadata and controls
216 lines (187 loc) · 6.29 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
207
208
209
210
211
212
213
214
215
216
/* 32. High-value customers
We want to send all of our high-value customers a
special VIP gift. We're defining high-value
customers as those who've made at least 1 order with
a total value (not including the discount) equal to
$10,000 or more. We only want to consider orders
made in the year 2016. */
Select
Customers.CustomerID
,Customers.CompanyName
,Orders.OrderID
,TotalOrderAmount = SUM(Quantity * UnitPrice)
From Customers
Join Orders
on Orders.CustomerID = Customers.CustomerID
Join OrderDetails
on Orders.OrderID = OrderDetails.OrderID
Where
OrderDate >= '20160101'
and OrderDate < '20170101'
Group by
Customers.CustomerID
,Customers.CompanyName
,Orders.Orderid
Having Sum(Quantity * UnitPrice) > 10000
Order by TotalOrderAmount DESC
/* 33. High-value customers - total orders
The manager has changed his mind. Instead of
requiring that customers have at least one individual
orders totaling $10,000 or more, he wants to define
high-value customers as those who have orders
totaling $15,000 or more in 2016. How would you
change the answer to the problem above? */
Select
Customers.CustomerID
,Customers.CompanyName
--,Orders.OrderID
,TotalOrderAmount = SUM(Quantity * UnitPrice)
From Customers
Join Orders
on Orders.CustomerID = Customers.CustomerID
Join OrderDetails
on Orders.OrderID = OrderDetails.OrderID
Where
OrderDate >= '20160101'
and OrderDate < '20170101'
Group by
Customers.CustomerID
,Customers.CompanyName
--,Orders.Orderid
Having sum(Quantity * UnitPrice) > 15000
Order by TotalOrderAmount desc;
/* 34. High-value customers - with discount
Change the above query to use the discount when
calculating high-value customers. Order by the total
amount which includes the discount.*/
Select
Customers.CustomerID
,Customers.CompanyName
,TotalsWithoutDiscount = SUM(Quantity * UnitPrice)
,TotalsWithDiscount = SUM(Quantity * UnitPrice * (1- Discount))
From Customers
Join Orders
on Orders.CustomerID = Customers.CustomerID
Join OrderDetails
on Orders.OrderID = OrderDetails.OrderID
Where
OrderDate >= '20160101'
and OrderDate < '20170101'
Group by
Customers.CustomerID
,Customers.CompanyName
Having sum(Quantity * UnitPrice * (1- Discount)) > 10000
Order by TotalsWithDiscount DESC;
/* 35. Month-end orders
At the end of the month, salespeople are likely to try
much harder to get orders, to meet their month-end
quotas. Show all orders made on the last day of the
month. Order by EmployeeID and OrderID */
Select
EmployeeID
,OrderID
,OrderDate
From Orders
Where OrderDate = EOMONTH(OrderDate )
Order by
EmployeeID
,OrderID
/* 36. Orders with many line items
The Northwind mobile app developers are testing an app that customers will use to show orders. In order
to make sure that even the largest orders will show up correctly on the app, they'd like some samples of
orders that have lots of individual line items. Show the 10 orders with the most line items, in order of
total line items */
Select top 10
Orders.OrderID
,TotalOrderDetails = count(*)
From Orders
Join OrderDetails
on Orders.OrderID = OrderDetails.OrderID
Group By Orders.OrderID
Order By count(*) desc
/* 37. Orders - random assortment
The Northwind mobile app developers would now like to just get a random assortment of orders for beta
testing on their app. Show a random set of 2% of all orders. */
Select top 2 percent
OrderID
From Orders
Order By NewID()
/* 38. Orders - accidental double-entry
Janet Leverling, one of the salespeople, has come to you with a request. She thinks that she accidentally
double-entered a line item on an order, with a different ProductID, but the same quantity. She
remembers that the quantity was 60 or more. Show all the OrderIDs with line items that match this, in order of OrderID */
Select OrderID, Quantity
From OrderDetails
Where Quantity >=60
Group by OrderID, Quantity
Having Count(*) > 1
/* 39. Orders - accidental double-entry details
Based on the previous question, we now want to show details of the order, for orders that match the above criteria */
With a as (
Select OrderID
From OrderDetails
Where Quantity >=60
Group by OrderID, Quantity
Having count (*) >1
)
Select OrderID, ProductID, UnitPrice, Quantity, Discount
From OrderDetails
Where OrderID IN (select OrderID from a)
Order by OrderID, Quantity
-- Cách 2
Select
OrderDetails.OrderID
,ProductID
,UnitPrice
,Quantity
,Discount
From OrderDetails
Join (
Select distinct
OrderID
From OrderDetails
Where Quantity >= 60
Group By OrderID, Quantity
Having Count(*) > 1
) PotentialProblemOrders
on PotentialProblemOrders.OrderID = OrderDetails.OrderID
Order by OrderID, ProductID
/* 41. Late orders
Some customers are complaining about their orders
arriving late. Which orders are late? */
Select OrderID,OrderDate, RequiredDate, ShippedDate
From Orders
Where ShippedDate >= RequiredDate
/* 42. Late orders - which employees?
Some salespeople have more orders arriving late than others. Maybe they're not following up on the order
process, and need more training. Which salespeople have the most orders arriving late? */
Select a.EmployeeID, b.LastName,
Total_OrderID_late = Count(*)
From Orders as a
join Employees as b
On a.EmployeeID = b.EmployeeID
Where a.ShippedDate >= a.RequiredDate
Group by a.EmployeeID, b.LastName
Order by Total_OrderID_late desc
/* 43. Late orders vs. total orders
Andrew, the VP of sales, has been doing some more thinking some more about the problem of late orders.
He realizes that just looking at the number of orders arriving late for each salesperson isn't a good idea. It
needs to be compared against the total number of orders per salesperson. Return results like the following: */
With Lateorders_table as(
Select EmployeeID, Total_Orders = Count(*)
From Orders
Where ShippedDate >= RequiredDate
Group by EmployeeID
)
, All_Orders_table as(
Select EmployeeID, Total_Orders = Count(*)
From Orders
Group by EmployeeID
)
Select Employees.EmployeeID, LastName, All_Orders = All_Orders_table.Total_Orders, Lateorders= Lateorders_table.Total_Orders
From Employees
Join All_Orders_table
On All_Orders_table.EmployeeID = Employees.EmployeeID
Join Lateorders_table
On Employees.EmployeeID = Lateorders_table.EmployeeID
Order by All_Orders, Lateorders desc