အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 11 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 1045. Customers Who Bought All Products ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ ကျွန်တော်တို့ကို Customer နဲ့ Product table နှစ်ခု ပေးထားပါတယ်။ Customer table ထဲမှာတော့ customer_id နဲ့ product_key ကတော့ foreign key column အနေနဲ့ Product table ထဲက column ကို reference လုပ်ထားပါတယ်။ Product table ထဲမှာတော့ product_key တစ်ခုပဲရှိပါတယ်။
1***Example 1:***
2
3Input:
4Customer table:
5+-------------+-------------+
6| customer_id | product_key |
7+-------------+-------------+
8| 1 | 5 |
9| 2 | 6 |
10| 3 | 5 |
11| 3 | 6 |
12| 1 | 6 |
13+-------------+-------------+
14Product table:
15+-------------+
16| product_key |
17+-------------+
18| 5 |
19| 6 |
20+-------------+
21Output:
22+-------------+
23| customer_id |
24+-------------+
25| 1 |
26| 3 |
27+-------------+
28***Explanation: ***
29The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.
30 ကျွန်တော်တို့ လိုချင်တဲ့ result ကတော့ Example ကအတိုင်း Product table ထဲမှာရှိတဲ့ product အကုန်လုံးကိုဝယ်တဲ့ customer IDs တွေကို ရှာပေးရမှာပဲဖြစ်ပါတယ်။ ကျွန်တော်ကတော့ အောက်ကအတိုင်း query လေးရေးဖြစ်ခဲ့ပါတယ်။
1SELECT c.customer_id FROM Customer c GROUP BY c.customer_id
2HAVING COUNT(DISTINCT c.product_key) = (SELECT COUNT(p.product_key) FROM Product p);အရင်ဆုံး ကျွန်တော် customer_id နဲ့ grouping လုပ်လိုက်ပါတယ်။ (Please Refrence Example Table) Customer တစ်ယောက်ချင်းစီ တွက်ချင်လို့ပဲဖြစ်ပါတယ်။ ဆိုကြပါစို့ ကျွန်တော်တို့မှာ customer 1 ကို group လုပ်လိုက်ရင် customer 1နဲ့ဆိုင်တဲ့ product key က 5, 6 ရှိနေမှာပါ။ ဒီနေရာမှာ customer ထဲက product_key ရဲ့ count ကို ယူလိုက်ချင်းအားဖြင့် customer ဝယ်ထားတဲ့ product အရေအတွက်ရမှာပဲဖြစ်ပါတယ်။ ကျွန်တော် Distinct သုံးတာကတော့ customer တစ်ယောက်က တူညီတဲ့ product တွေကိုဝယ်နိုင်လို့ပဲဖြစ်ပါတယ်။ distinct ထည့်လိုက်ခြင်းအားဖြင့်တူရင် တစ်ခုပဲယူဆလိုက်တာပေါ့။
ရလာတဲ့ result ကို Product table ထဲမှာရှိတဲ့ product တွေရဲ့ အရေအတွက်နဲ့ နှိုင်းယှဉ်လို့ ညီတယ်ဆိုရင် customer က product အကုန်ဝယ်ထားတဲ့ customer လို့ ယူဆနိုင်တာပဲဖြစ်ပါတယ်။ ဒါဆို Day 11 အတွက်လည်း အဆင်ပြေသွားခဲ့ပါပြီဗျာ။
Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 11 - 1045. Customers Who Bought All Products)
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


