အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 16 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 1070. Product Sales Analysis III ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ Sales ဆိုတဲ့ table လေးပေးထားပါတယ်။ ဒီ table ထဲမှာဆို sale_id, product_id, year, quantity, price ဆိုတဲ့ column လေးတွေပေးထားပါတယ်။
1Example 1:
2
3Input:
4Sales table:
5+---------+------------+------+----------+-------+
6| sale_id | product_id | year | quantity | price |
7+---------+------------+------+----------+-------+
8| 1 | 100 | 2008 | 10 | 5000 |
9| 2 | 100 | 2009 | 12 | 5000 |
10| 7 | 200 | 2011 | 15 | 9000 |
11+---------+------------+------+----------+-------+
12
13Output:
14+------------+------------+----------+-------+
15| product_id | first_year | quantity | price |
16+------------+------------+----------+-------+
17| 100 | 2008 | 10 | 5000 |
18| 200 | 2011 | 15 | 9000 |
19+------------+------------+----------+-------+
20 ကျွန်တော်တို့ လုပ်ပေးရမှာကတော့ product တွေမှာ ရောင်းတဲ့ နှစ်တွေအများကြီးရှိတာမို့ product တစ်ခုချင်းစီအတွက် အစောဆုံးနှစ်မှာရောင်းခဲ့တဲ့ data တွေကို ရှာပေးရမှာပဲဖြစ်ပါတယ်။
ကျွန်တော်ကတော့ အောက်က query လေးအတိုင်းရေးဖြစ်ခဲ့ပါတယ်။
1SELECT product_id,
2year AS first_year,
3quantity,
4price FROM Sales
5WHERE (product_id, year) IN (
6 SELECT product_id, MIN(year) FROM Sales
7GROUP BY product_id
8);ဒီ မှာဆို အရင်ဆုံးsub query လေးမှာ ကျွန်တော်ကတော့ product တစ်ခုချင်းစီအတွက် record ရအောင် product id နဲ့ Group By လုပ်လိုက်ပါတယ်။ အဲ့ ရလာတဲ့ product data ထဲကမှ အစောဆုံး နှစ်ကိုရအောင် MIN Function သုံးပြီးအငယ်ဆုံးနှစ်ရယ် အဲ့ နှစ်မှာရောင်းခဲ့တဲ့ product id တွေရယ်ကို ရှာလိုက်ပါတယ်။ Main query ကြီးမှာတော့ sub query ကရလာတဲ့ product IDs တွေရယ် year ရယ်တူနေတဲ့ record တွေကို ရှာဖို့ where condition မှာစစ်ပေးလိုက်ပါတယ်။ ဒါဆိုကျွန်တော်တို့လိုချင်တဲ့ product တစ်ခုချင်းစီဖို့ အစောဆုံးနှစ် ရောင်းခဲ့တဲ့ data တွေရရှိပြီပဲဖြစ်ပါတယ်ခင်ဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 16 - 1070. Product Sales Analysis III
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


