File tree 6 files changed +162
-4
lines changed
1164.Product Price at a Given Date
6 files changed +162
-4
lines changed Original file line number Diff line number Diff line change @@ -114,7 +114,30 @@ id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品
114
114
### ** SQL**
115
115
116
116
``` sql
117
-
117
+ # Write your MySQL query statement below
118
+ select
119
+ u .user_id as seller_id,
120
+ case
121
+ when u .favorite_brand = i .item_brand then ' yes'
122
+ else ' no'
123
+ end as 2nd_item_fav_brand
124
+ from
125
+ users u
126
+ left join (
127
+ select
128
+ order_date,
129
+ item_id,
130
+ seller_id,
131
+ rank() over(
132
+ partition by seller_id
133
+ order by
134
+ order_date
135
+ ) as rk
136
+ from
137
+ orders
138
+ ) o on u .user_id = o .seller_id
139
+ and o .rk = 2
140
+ left join items i on o .item_id = i .item_id ;
118
141
```
119
142
120
143
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -115,7 +115,30 @@ The answer for the user with id 4 is no because the brand of their second sold i
115
115
### ** SQL**
116
116
117
117
``` sql
118
-
118
+ # Write your MySQL query statement below
119
+ select
120
+ u .user_id as seller_id,
121
+ case
122
+ when u .favorite_brand = i .item_brand then ' yes'
123
+ else ' no'
124
+ end as 2nd_item_fav_brand
125
+ from
126
+ users u
127
+ left join (
128
+ select
129
+ order_date,
130
+ item_id,
131
+ seller_id,
132
+ rank() over(
133
+ partition by seller_id
134
+ order by
135
+ order_date
136
+ ) as rk
137
+ from
138
+ orders
139
+ ) o on u .user_id = o .seller_id
140
+ and o .rk = 2
141
+ left join items i on o .item_id = i .item_id ;
119
142
```
120
143
121
144
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ select
3
+ u .user_id as seller_id,
4
+ case
5
+ when u .favorite_brand = i .item_brand then ' yes'
6
+ else ' no'
7
+ end as 2nd_item_fav_brand
8
+ from
9
+ users u
10
+ left join (
11
+ select
12
+ order_date,
13
+ item_id,
14
+ seller_id,
15
+ rank() over(
16
+ partition by seller_id
17
+ order by
18
+ order_date
19
+ ) as rk
20
+ from
21
+ orders
22
+ ) o on u .user_id = o .seller_id
23
+ and o .rk = 2
24
+ left join items i on o .item_id = i .item_id ;
Original file line number Diff line number Diff line change @@ -62,7 +62,36 @@ Products</code> 表:
62
62
### ** SQL**
63
63
64
64
``` sql
65
-
65
+ # Write your MySQL query statement below
66
+ select
67
+ p1 .product_id product_id,
68
+ ifnull(p2 .price , 10 ) price
69
+ from
70
+ (
71
+ select
72
+ distinct(product_id) product_id
73
+ from
74
+ Products
75
+ ) p1
76
+ left join (
77
+ select
78
+ t1 .product_id ,
79
+ t1 .new_price price
80
+ from
81
+ Products t1
82
+ join (
83
+ select
84
+ product_id,
85
+ max (change_date) change_date
86
+ from
87
+ Products
88
+ where
89
+ change_date <= ' 2019-08-16'
90
+ group by
91
+ product_id
92
+ ) t2 on t1 .product_id = t2 .product_id
93
+ and t1 .change_date = t2 .change_date
94
+ ) p2 on p1 .product_id = p2 .product_id ;
66
95
```
67
96
68
97
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -58,7 +58,36 @@ Products table:
58
58
### ** SQL**
59
59
60
60
``` sql
61
-
61
+ # Write your MySQL query statement below
62
+ select
63
+ p1 .product_id product_id,
64
+ ifnull(p2 .price , 10 ) price
65
+ from
66
+ (
67
+ select
68
+ distinct(product_id) product_id
69
+ from
70
+ Products
71
+ ) p1
72
+ left join (
73
+ select
74
+ t1 .product_id ,
75
+ t1 .new_price price
76
+ from
77
+ Products t1
78
+ join (
79
+ select
80
+ product_id,
81
+ max (change_date) change_date
82
+ from
83
+ Products
84
+ where
85
+ change_date <= ' 2019-08-16'
86
+ group by
87
+ product_id
88
+ ) t2 on t1 .product_id = t2 .product_id
89
+ and t1 .change_date = t2 .change_date
90
+ ) p2 on p1 .product_id = p2 .product_id ;
62
91
```
63
92
64
93
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # Write your MySQL query statement below
2
+ select
3
+ p1 .product_id product_id,
4
+ ifnull(p2 .price , 10 ) price
5
+ from
6
+ (
7
+ select
8
+ distinct(product_id) product_id
9
+ from
10
+ Products
11
+ ) p1
12
+ left join (
13
+ select
14
+ t1 .product_id ,
15
+ t1 .new_price price
16
+ from
17
+ Products t1
18
+ join (
19
+ select
20
+ product_id,
21
+ max (change_date) change_date
22
+ from
23
+ Products
24
+ where
25
+ change_date <= ' 2019-08-16'
26
+ group by
27
+ product_id
28
+ ) t2 on t1 .product_id = t2 .product_id
29
+ and t1 .change_date = t2 .change_date
30
+ ) p2 on p1 .product_id = p2 .product_id ;
You can’t perform that action at this time.
0 commit comments