အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ 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
Day 1 - Software Development ကို ပိုပြီးမြန်စေမယ့် CI/CD
Software တစ်ခုရေးပြီးပြီဆိုရင် "ငါ့စက်ထဲမှာတော့ အလုပ်လုပ်တယ်" ဆိုရုံနဲ့ မပြီးသေးပါဘူး။ User တွေသုံးမယ့် Server ပေါ်ရောက်...

Secure AWS ECR Github Action Using OIDC
Modern DevOps နှင့် Cloud Security တို့မှာ အဓိကဖြစ်လာသည့် Authentication System ယနေ့ခေတ် DevOps, Cloud Engineering, CI/C...

Manual vs Automated Infrastructure: Why "useradd" Still Matters in the Terraform Era
DevOps careerကို လျှောက်နေတဲ့သူတိုင်း Terraform၊ Ansible စတဲ့ Infrastructure as Code (IaC) tools တွေရဲ့ အလုပ်လုပ်နိုင်စွ...
From Surviving to Thriving
“AI ကြောင့် developer အလုပ်ပျောက်သွားမလား?” ဆိုတဲ့ မေးခွန်းကို Junior developer တိုင်း စဉ်းစားဖူးကြမှာပါ။ ဈေးကွက်အခြေအနေ...

AWS - Global Infra
AWS Global Infra AWS Global Infra & Service Type ဆိုတဲ့ ခေါင်းစဉ်နဲ့ ၂၀၂၅ ဒီဇင်ဘာမှာရေးထားဖူးတဲ့ article ကို အရင်ဆုံး ဖတ...

