-
Notifications
You must be signed in to change notification settings - Fork 0
/
Danny's Dinner.sql
326 lines (277 loc) · 7.33 KB
/
Danny's Dinner.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
--refrence: https://8weeksqlchallenge.com/case-study-1/
--All questions and database are available in above link
--linkedin hastag: #8WeekSQLChallenge
--Q1
--Join sales and menu tables, then sum all prices per each customer_id
select
sales.customer_id,
sum(menu.price) as total_sales
from sales
left join menu
on sales.product_id = menu.product_id
group by sales.customer_id
order by total_sales desc;
--Q2
--Count distinct order_date per each customer_id
select
sales.customer_id,
count(distinct sales.order_date) as visited_days
from sales
group by sales.customer_id
order by visited_days desc;
--Q3
--Join sales and menu tables, then order by date and add index to them based of order_date per each customer
--Put all above into temp table, then filter it on first index
drop table if exists #row_table
create table #row_table(
customer_id varchar(50),
order_date date,
product_name varchar(50),
row_num int
)
insert into #row_table (customer_id,order_date,product_name,row_num )
select
sales.customer_id,
sales.order_date,
menu.product_name,
ROW_NUMBER() over(partition by sales.customer_id order by order_date) as row_num
from sales
left join menu
on sales.product_id = menu.product_id
order by order_date
select * from #row_table
where row_num = 1;
--Q4
--Join menu and sales tables, then count all products and sort them
--Filter total_count on max using temp table
drop table if exists #max_purchase
create table #max_purchase(
product_name varchar(50),
total_count int
)
insert into #max_purchase (product_name,total_count)
select
menu.product_name,
count(menu.product_name) as total_count
from menu
left join sales
on menu.product_id = sales.product_id
group by menu.product_name
order by total_count desc
select * from #max_purchase
where total_count = (select
max(total_count)
from #max_purchase
)
--Q5
--First we make temp table to add count of order per product and customer
--Then make another temp table to add maximum number of order per each customer and filter it to this number
drop table if exists #sales_menu
create table #sales_menu(
customer_id varchar(50),
order_date date,
product_name varchar(50),
price int
)
insert into #sales_menu (customer_id, order_date, product_name, price)
select
sales.customer_id,
sales.order_date,
menu.product_name,
menu.price
from sales
left join menu
on sales.product_id = menu.product_id;
drop table if exists #q5
create table #q5 (
customer_id varchar(50),
product_name varchar(50),
order_count int
)
insert into #q5
select
#sales_menu.customer_id as customer_id,
#sales_menu.product_name as product_name,
count(#sales_menu.product_name) as order_count
from #sales_menu
group by customer_id, product_name
drop table if exists #q5_1
create table #q5_1 (
customer_id varchar(50),
product_name varchar(50),
order_count int,
mx int
)
insert into #q5_1
select *,
max(order_count) over(partition by customer_id) as mx
from #q5
select
customer_id,
product_name,
order_count
from #q5_1
where order_count = mx
--Q6
--Joint all 3 tables
--Filter it based on grater order date than join date per customer
drop table if exists #all_table
create table #all_table
(
customer_id varchar(50),
join_date date,
product_name varchar(50),
price int,
order_date date
)
insert into #all_table
select
#sales_menu.customer_id,
members.join_date,
product_name,
price,
order_date
from #sales_menu
left join members
on #sales_menu.customer_id = members.customer_id
drop table if exists #q6
create table #q6
(customer_id varchar(50),
join_date date,
product_name varchar(50),
price int,
order_date date,
row_num int
)
insert into #q6
select * ,
ROW_NUMBER() over(partition by customer_id order by order_date) as row_num
from #all_table
where order_date > join_date
order by order_date
select
customer_id,
order_date,
product_name
from #q6
where row_num = 1
--Q7
--Just opposite of Q6 steps
drop table if exists #q7
create table #q7
(customer_id varchar(50),
join_date date,
product_name varchar(50),
price int,
order_date date,
row_num int
)
insert into #q7
select * ,
ROW_NUMBER() over(partition by customer_id order by order_date desc) as row_num
from #all_table
where order_date < join_date
order by order_date
select
customer_id,
order_date,
product_name
from #q7
where row_num = 1
--Q8
--Use #q7 to find transaction before becoming a member then aggregate them
select
customer_id,
count(product_name) as total_item,
sum(price) as total_spent
from #q7
group by customer_id
--Q9
--Write conditional statement using product_name then sum all scores per customer
select
customer_id,
sum(case
when product_name = 'sushi' then 20
else 10
end * price) as total_score
from #sales_menu
group by customer_id
order by total_score desc;
--Q10
--Add another condition to Q9 case statement then filter customers and order date
select
customer_id ,
sum(case
when order_date between join_date and DATEADD(day,7,join_date) then 20
when product_name = 'sushi' then 20
else 10
end * price) as total_score
from #all_table
where customer_id in ('A','B') and order_date <= '2021-01-30'
group by customer_id
order by total_score desc;
--BQ1
--Create temp table from #all_table by setting condition on order date
drop table if exists #BQ1
create table #BQ1 (
transaction_id int,
customer_id varchar(50),
order_date date,
product_name varchar(50),
price int,
member varchar(50),
)
insert into #BQ1
select
ROW_NUMBER() over(order by order_date) as transaction_id,
customer_id,
order_date,
product_name,
price,
case
when order_date >= join_date then 'Y'
else 'N'
end as member
from #all_table
select * from #BQ1
order by customer_id
--BQ2
--Filter null #BQ1 on Y, then rank them and join them to #BQ1
--Select needed columns from previuos temp table (#initial_BQ2)
drop table if exists #initial_BQ2
create table #initial_BQ2 (
transaction_id int,
customer_id varchar(50),
order_date date,
product_name varchar(50),
price int,
member varchar(50),
transaction_id_1 int,
ranking int,
ranking_1 int
)
insert into #initial_BQ2
select
*,
case
when #BQ1.member = 'N' then null
else ranking
end as ranking_1
from
#BQ1
left join
(select
transaction_id as transaction_id_1,
RANK() over(partition by customer_id order by order_date) as ranking
from #BQ1
where member = 'Y') as t
on t.transaction_id_1 = #BQ1.transaction_id
select
customer_id,
order_date,
product_name,
price,
member,
ranking
from #initial_BQ2
order by customer_id, order_date