အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 17 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ **585. Investments in 2016 **ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ Insurance ဆိုတဲ့ table လေးကို ပေးထားပြီး *pid , tiv_2015 , tiv_2016 , lat ,lon ** Column လေးတွေပါဝင်ပါတယ်။ pid ကတော့ primary key ဖြစ်ပြီး tiv *တွေကတော့ total investment value တွေဖြစ်ပါတယ်။ *lat , lon *ကတော့ latitude နဲ့ longitude ဖြစ်ပါတယ်။
ကျွန်တော်တို့ က criteria 2 ခုနဲ့ကိုက်ညီတဲ့ total investment value in 2016 ကိုရှာပေးရမှာဖြစ်ပါတယ်။ "ပထမတစ်ခုကတော့ tiv_2015 value တွေတူညီရမှာဖြစ်ပြီး နောက်တစ်ခုကတော့ lat, lon က တစ်ခြား policy holder တွေနဲ့မတူဘဲ unique ဖြစ်နေရမှာပါ။ ရလာတဲ့ result ကိုလည်း decimal 2 နေရာစာ round လုပ်ပေးရမှာပဲဖြစ်ပါတယ်။"
1Example 1:
2
3Input:
4Insurance table:
5+-----+----------+----------+-----+-----+
6| pid | tiv_2015 | tiv_2016 | lat | lon |
7+-----+----------+----------+-----+-----+
8| 1 | 10 | 5 | 10 | 10 |
9| 2 | 20 | 20 | 20 | 20 |
10| 3 | 10 | 30 | 20 | 20 |
11| 4 | 10 | 40 | 40 | 40 |
12+-----+----------+----------+-----+-----+
13Output:
14+----------+
15| tiv_2016 |
16+----------+
17| 45.00 |
18+----------+
19Explanation:
20The first record in the table, like the last record, meets both of the two criteria.
21The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique.
22
23The second record does not meet any of the two criteria. Its tiv_2015 is not like any other policyholders and its location is the same as the third record, which makes the third record fail, too.
24So, the result is the sum of tiv_2016 of the first and last record, which is 45.
25 သူပေးထားတဲ့ example မှာဆိုရင် tiv_2015တူတဲ့ record 3 ခုရှိနေပါတယ်။ ဒါပေမဲ့ id 3 ရဲ့ lat, lon က id 2 ရဲ့ lat, lon နဲ့တူနေတာကြောင့် ignore လုပ်လိုပ်တော့ id 1 and 4 record 2ခုက လိုချင်တဲ့ criteria နဲ့ကိုက်ညီနေပါတယ်။ သူတို့ရဲ့ tiv_2016 ပေါင်းလဒ်က 45 ရပြီး decimal 2 နေရာစာ round လုပ်ပေးဖို့လိုတော့ 45.00 ရလာပါတယ်။ ကျွန်တော်ကတော့ အောက်ကအတိုင်း Query လေးရေးဖြစ်ခဲ့ပါတယ်။
1SELECT ROUND(SUM(tiv_2016), 2) AS tiv_2016
2FROM Insurance
3 WHERE tiv_2015 IN (
4 SELECT tiv_2015 FROM Insurance
5 GROUP BY tiv_2015 HAVING COUNT(pid) > 1)
6AND (lat, lon) IN (
7 SELECT lat, lon FROM Insurance
8 GROUP BY lat, lon HAVING COUNT(pid) = 1
9 );ဒီ query လေးမှာဆို အရင်ဆုံး ကျွန်တော်တို့ က tiv_2016 ရဲ့ပေါင်းလဒ်လိုချင်တာဖြစ်လို့ SUM function လေးနဲ့ပေါင်းပြီး Decimal 2 နေရာစာ ROUND လုပ်လိုက်ပါတယ်။ Where နောက်က condition လေးမှာတော့ ကျွန်တော်တို့ လိုချင်တဲ့ criteria နဲ့ညီအောင်စစ်ပေးလိုက်ပါတယ်။ ပထမ တစ်ချက်အနေနဲ့ tiv_2015 တူရပါမယ်။ ကျွန်တော်က တော့ sub query လေးရေးပြီး tiv_2015 နဲ့ Group By လုပ်လိုက်ပြီး Count က 1 ထက်ကြီးရမယ်ဆိုစစ်ထားပါတယ်။ Example ပြထားတဲ့ table ကိုကြည့်မယ်ဆို tiv_2015 နဲ့ group by လုပ်ရင် tiv_2015 value 10 ဖို့က record 3 ခုထွက်လာမှာဖြစ်ပြီး 20 ဖို့က record 1 ခုထဲထွက်လာမှာပါ။ ကျွန်တော်တို့ record က 1ခုထက်ပိုရမယ်လို့စစ်လိုက်ခြင်းဖြင့် လိုချင်တဲ့ tiv 2015 value တူတဲ့ record တွေရလာမှာပဲဖြစ်ပါတယ်။
1**tiv_2015 GROUP BY**
2 10 -> count = 3
3 20 -> count = 1ဒုတိယ criteria အတွက်လည်း ကျွန်တော်က sub query လေးရေးပြီး lon, lat နဲ့ Group By လုပ်ပြီး Count ကို 1နဲ့ညီရမယ်လို့စစ်ထားလိုက်ပါတယ်။ Example table ကိုပြန်ကြည့်မယ်ဆို lat, long group by လုပ်ရင်
1**lat, lon GROUP BY**
2 10,10 -> count = 1
3 20,20 -> count = 2
4 40,40 -> count = 1ဆိုပြီးထွက်လာမှာပါ Count က 1 ဆိုရင် တူတဲ့ value မရှိတဲ့အတွက် unique ဖြစ်ပါတယ်။ ဒါဆို သူလိုချင်တဲ့ အချက်တွေနဲ့ ကိုက်ညီပြီး problem လေးက လိုချင်တဲ့အဖြေထွက်လာပြီပဲဖြစ်ပါတယ်။
Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 17 - 585. Investments in 2016
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


