အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 13 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 1193. Monthly Transactions I ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ ကျွန်တော်တို့ကို Transactions table တစ်ခုထဲပေးထားပြီး id, country, state, amount နဲ့ trans date column တွေပါတာပဲဖြစ်ပါတယ်။ state ကတော့ enum value ဖြစ်ပြီး (approved, declined) ဆိုပြီး value ရှိမှာဖြစ်ပါတယ်။ ကျွန်တော်တို့လုပ်ပေးဖို့လိုတာကတော့ လတစ်လချင်းစီရယ် country တစ်ခုချင်းစီရဲ့ လိုချင်တဲ့ data တွေကို တွက်ထုတ်ပေးရမှာပဲဖြစ်ပါတယ်။ သူပေးထာတဲ့ Example လေးကိုကြည့်ရအောင်ဗျာ။
1**Example** 1:
2
3Input:
4Transactions table:
5+------+---------+----------+--------+------------+
6| id | country | state | amount | trans_date |
7+------+---------+----------+--------+------------+
8| 121 | US | approved | 1000 | 2018-12-18 |
9| 122 | US | declined | 2000 | 2018-12-19 |
10| 123 | US | approved | 2000 | 2019-01-01 |
11| 124 | DE | approved | 2000 | 2019-01-07 |
12+------+---------+----------+--------+------------+
13Output:
14+----------+---------+-------------+----------------+--------------------+-----------------------+
15| month | country | trans_count | approved_count | trans_total_amount | approved_total_amount |
16+----------+---------+-------------+----------------+--------------------+-----------------------+
17| 2018-12 | US | 2 | 1 | 3000 | 1000 |
18| 2019-01 | US | 1 | 1 | 2000 | 2000 |
19| 2019-01 | DE | 1 | 1 | 2000 | 2000 |
20+----------+---------+-------------+----------------+--------------------+-----------------------+
21 ဒီမှာဆို US က transaction 3 ကြောင်းရှိနေပြီး 2018-12 လပိုင်းမှာနှစ်ကြောင်းနဲ့ 2019-01 မှာ တစ်ကြောင်းထဲရှိတာတွေ့ရမှာပဲဖြစ်ပါတယ်။ ကျွန်တော်တို့ output မှာဆို 12 လပိုင်းအတွက် US က total transaction က 2 ကြောင်း approved transaction က တစ်ကြောင်းနဲ့ သတ်ဆိုင်ရာ amount ပေါင်းလဒ်တွေလည်းတွေ့ရမှာပဲဖြစ်ပါတယ်။ ကျွန်တော်ကတော့ အောက်ကအတိုင်း query လေး ရေးဖြစ်ခဲ့ပါတယ်ဗျာ။
1SELECT
2DATE_FORMAT(trans_date, '%Y-%m') AS month,
3country,
4COUNT(id) AS trans_count,
5SUM(state = 'approved') AS approved_count,
6SUM(amount) AS trans_total_amount,
7SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount
8FROM Transactions GROUP BY month, country;အဓိက လိုအပ်တာကတော့ ကျွန်တော်တို့ month ရော country ရော နဲ့ GROUP BY လုပ်ပေးရမှာပဲဖြစ်ပါတယ်။ ဒါမှသူလိုချင်တဲ့အတိုင်း လတစ်လချက်းစီ နိုင်ငံတစ်နိုင်ငံချင်းစီရဲ့ data တွေရရှိမှာပဲဖြစ်ပါတယ်။ month column ကတော့ format ချထားတဲ့ပုံစံပဲဖြစ်တာကြောင့် MONTH function မသုံးဘဲ DATE_FORMAT function လေးပဲသုံးဖြစ်ခဲ့ပါတယ်ဗျ။ ကျန်တာတွေကတော့ အရင်ကပုစ္ဆာတွေအတိုင်း SUM နဲ့ COUNT function လေးတွေသုံးပြီး condition စစ်ပေးလိုက်ခြင်းဖြင့် အဆင်ပြေသွားခဲ့ပြီပဲဖြစ်ပါတယ်ဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 13 - 1193. Monthly Transactions 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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


