Skip to content

Commit ead6d2a

Browse files
committed
feat: add solutions to lc problems: No.1159,1164
* No.1159.Market Analysis II * No.1164.Product Price at a Given Date
1 parent bc77796 commit ead6d2a

File tree

6 files changed

+162
-4
lines changed

6 files changed

+162
-4
lines changed

solution/1100-1199/1159.Market Analysis II/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,30 @@ id为 4 的用户的查询结果是 no,因为他卖出的第二件商品的品
114114
### **SQL**
115115

116116
```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;
118141
```
119142

120143
<!-- tabs:end -->

solution/1100-1199/1159.Market Analysis II/README_EN.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,30 @@ The answer for the user with id 4 is no because the brand of their second sold i
115115
### **SQL**
116116

117117
```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;
119142
```
120143

121144
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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;

solution/1100-1199/1164.Product Price at a Given Date/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,36 @@ Products</code> 表:
6262
### **SQL**
6363

6464
```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;
6695
```
6796

6897
<!-- tabs:end -->

solution/1100-1199/1164.Product Price at a Given Date/README_EN.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,36 @@ Products table:
5858
### **SQL**
5959

6060
```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;
6291
```
6392

6493
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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;

0 commit comments

Comments
 (0)