အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 14 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 1158. Market Analysis I ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။
1Example 1:
2
3Input:
4Users table:
5+---------+------------+----------------+
6| user_id | join_date | favorite_brand |
7+---------+------------+----------------+
8| 1 | 2018-01-01 | Lenovo |
9| 2 | 2018-02-09 | Samsung |
10| 3 | 2018-01-19 | LG |
11| 4 | 2018-05-21 | HP |
12+---------+------------+----------------+
13Orders table:
14+----------+------------+---------+----------+-----------+
15| order_id | order_date | item_id | buyer_id | seller_id |
16+----------+------------+---------+----------+-----------+
17| 1 | 2019-08-01 | 4 | 1 | 2 |
18| 2 | 2018-08-02 | 2 | 1 | 3 |
19| 3 | 2019-08-03 | 3 | 2 | 3 |
20| 4 | 2018-08-04 | 1 | 4 | 2 |
21| 5 | 2018-08-04 | 1 | 3 | 4 |
22| 6 | 2019-08-05 | 2 | 2 | 4 |
23+----------+------------+---------+----------+-----------+
24Items table:
25+---------+------------+
26| item_id | item_brand |
27+---------+------------+
28| 1 | Samsung |
29| 2 | Lenovo |
30| 3 | LG |
31| 4 | HP |
32+---------+------------+
33Output:
34+-----------+------------+----------------+
35| buyer_id | join_date | orders_in_2019 |
36+-----------+------------+----------------+
37| 1 | 2018-01-01 | 1 |
38| 2 | 2018-02-09 | 2 |
39| 3 | 2018-01-19 | 0 |
40| 4 | 2018-05-21 | 0 |
41+-----------+------------+----------------+
42 ဒီပုစ္ဆာ အတွက်ကတော့ ကျွန်တော်တို့ကို Users, Orders နဲ့ Items ဆိုတဲ့ table သုံးခုပေးထားပါတယ်။ Users table ထဲမှာတော့ id , join date နဲ့ favourite brand ဆိုတဲ့ column လေးတွေပါဝင်ပါတယ်။ Orders table ထဲမှာကတော့ order_id, order_date, item_id, buyer_id, seller_id ဆိုတဲ့ column တွေပါဝင်ပါတယ်။ Users တွေကတော့ buyer ကော seller ကောဖြစ်နိုင်တဲ့အတွက် Orders table ထဲမှာ foreign key အနေနဲ့ reference လုပ်ထားပါတယ်။ ကျွန်တော်တို့ ရှာပေးရမှာက user တစ်ယောက်ချင်းစီရဲ့ id, join date, နဲ့ 2019ခုနှစ်အတွင်း buyer အနေနဲ့ လုပ်ဖြစ်ခဲ့တဲ့ order အရေအတွက် orders in 2019 ကိုရှာပေးရမှာပဲဖြစ်ပါတယ်။
ကျွန်တော်ကတော့အောက်ကအတိုင်းလေးရေးဖြစ်ခဲ့ပါတယ်။
1SELECT
2u.user_id AS buyer_id,
3u.join_date,
4COUNT(
5 CASE WHEN YEAR(order_date) = 2019 THEN o.order_id END
6) AS orders_in_2019
7FROM Users u LEFT JOIN Orders o ON o.buyer_id = u.user_id
8GROUP BY u.user_id, u.join_date;ကျွန်တော်တို့က user တစ်ယောက်ချင်းစီရဲ့ record ကိုလိုချင်တာဖြစ်လို့ user id , joined date နဲ့ group by လုပ်ထားလိုက်ပါတယ်။ buyer အနေနဲ့ user တွေကို လိုချင်တာဖြစ်လို့ Order table နဲ့ Left join join လိုက်ပါတယ်။ ဒါဆိုရင် ကျွန်တော်တို့အဓိကတွက်ပေးဖို့လိုအပ်တာက orders in 2019ပါ buyer အနေနဲ့က Join လိုက်ထဲကစစ်ပြီးပြီဆိုတော့ ကျွန်တော်တို့ order date က 2019ဖြစ်တဲ့ order တွေရဲ့ id ကို COUNT Function လေးနဲ့ ရေတွက်ပေးလိုက်ခြင်းဖြင့် problem လေးကတော့ အဆင်ပြေသွားခဲ့ပြီပဲဖြစ်ပါတယ်ဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 14 - 1158. Market Analysis I
Watch on YouTubeDiscussion
Join the conversation
How do you feel about this article?
Comments
Sign in to join the conversation
Sign in to be the first to comment!
Share Your Article
Share with your professional network
Recent Articles

AWS - Application Load Balancer
Elastic Load Balancing (ELB) ELB ဆိုတာကတော့ request တွေကို တစ်နေရာတည်းမှ လက်ခံကာ Amazon EC2 instances၊ containers, etc.....

Terraform Day 3: Benefits of Terraform State
Terraform ကိုလေ့လာ တဲ့အခါ ကျွန််တော်တို့ရဲ့ Project Folder ထဲမှာ terraform.tfstate ဆိုတဲ့ ဖိုင်လေးကို တွေ့ဖူးကြပါလိမ့်မယ...

Terraform Day 2: Essential IaC Principles You Must Know
မနေ့ကတော့ Terraform အကြောင်း အကြမ်းဖျင်း Concept ကို ပြောပြခဲ့ပြီးပြီဆိုတော့ ဒီနေ့မှာတော့ Terraform ကို Professional ကျက...

TCP/IP Protocol
အားလုံးပဲမင်္ဂလာပါ။ ဒီနေ့ ကျွန်တော်တို့ TCP/IP Protocol အကြောင်း ဆွေးနွေးသွားပါမယ်။ ပထမဆုံးအနေနဲ့ TCP/IP ရဲ့ History လေး...

Terraform Day 1: Introduction to IAC and Terraform
ကျွန်တော်တို့ cloud အကြောင်း စပြောကြပြီဆိုရင် အရင်ဆုံး ခေါင်းထဲရောက်လာတာ Console ထဲဝင်၊ UI ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


