အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 25 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 1321. Restaurant Growth ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ မှာတော့ Customer ဆိုတဲ့ table လေးကိုပေးထားပါတယ်။
1Table: Customer2 3+---------------+---------+4| Column Name | Type |5+---------------+---------+6| customer_id | int |7| name | varchar |8| visited_on | date |9| amount | int |10+---------------+---------+11In SQL,(customer_id, visited_on) is the primary key for this table.12This table contains data about customer transactions in a restaurant.13visited_on is the date on which the customer with ID (customer_id) has visited the restaurant.14amount is the total paid by a customer.ကျွန်တော်တို့လုပ်ပေးဖို့လိုတာကတော့ visited_on က လွန်ခဲ့တဲ့ 7 ရက်ရှိတဲ့ transaction တွေရဲ့ စုစုပေါင်း amount နဲ့ average amount ကိုရှာပေးရမှာပဲဖြစ်ပါတယ်။ average amount ကိုတော့ decimal 2 နေရာစာဖြတ်ပေးပြီး ရရှိလာတဲ့ record တွေကိုတော့ visited on ကို ASC order စီပေးရမှာပဲဖြစ်ပါတယ်။
1Example 1:2 3Input: 4Customer table:5+-------------+--------------+--------------+-------------+6| customer_id | name | visited_on | amount |7+-------------+--------------+--------------+-------------+8| 1 | Jhon | 2019-01-01 | 100 |9| 2 | Daniel | 2019-01-02 | 110 |10| 3 | Jade | 2019-01-03 | 120 |11| 4 | Khaled | 2019-01-04 | 130 |12| 5 | Winston | 2019-01-05 | 110 | 13| 6 | Elvis | 2019-01-06 | 140 | 14| 7 | Anna | 2019-01-07 | 150 |15| 8 | Maria | 2019-01-08 | 80 |16| 9 | Jaze | 2019-01-09 | 110 | 17| 1 | Jhon | 2019-01-10 | 130 | 18| 3 | Jade | 2019-01-10 | 150 | 19+-------------+--------------+--------------+-------------+20Output: 21+--------------+--------------+----------------+22| visited_on | amount | average_amount |23+--------------+--------------+----------------+24| 2019-01-07 | 860 | 122.86 |25| 2019-01-08 | 840 | 120 |26| 2019-01-09 | 840 | 120 |27| 2019-01-10 | 1000 | 142.86 |28+--------------+--------------+----------------+29Explanation: 301st moving average from 2019-01-01 to 2019-01-07 has an average_amount of (100 + 110 + 120 + 130 + 110 + 140 + 150)/7 = 122.86312nd moving average from 2019-01-02 to 2019-01-08 has an average_amount of (110 + 120 + 130 + 110 + 140 + 150 + 80)/7 = 120323rd moving average from 2019-01-03 to 2019-01-09 has an average_amount of (120 + 130 + 110 + 140 + 150 + 80 + 110)/7 = 120334th moving average from 2019-01-04 to 2019-01-10 has an average_amount of (130 + 110 + 140 + 150 + 80 + 110 + 130 + 150)/7 = 142.8634 ကျွန်တော်ကတော့အောက်ကအတိုင်းရေးဖြစ်ခဲ့ပါတယ်။
1**SQL**2 3WITH daily AS (4 SELECT visited_on , SUM(amount) AS daily_amount5 FROM Customer GROUP BY visited_on6)7SELECT 8 d1.visited_on,9 SUM(d2.daily_amount) AS amount,10 ROUND(SUM(d2.daily_amount) / 7 , 2) AS average_amount11FROM daily d112JOIN daily d213 ON d2.visited_on BETWEEN DATE_SUB(d1.visited_on, INTERVAL 6 DAY) 14 AND d1.visited_on15GROUP BY d1.visited_on HAVING COUNT(d1.visited_on) = 716ORDER BY visited_on;ပထမဆုံးအနေနဲ့ daily transaction တွေဖို့ temporary result table လေးထုတ်လိုက်ပါတယ်။ဒီ table ကိုတော့ daily လို့ပေးပြီး အဲ့ table လေးကိုအသုံးပြုပြီးရှာမှာပဲဖြစ်ပါတယ်။
လွန်ခဲ့တဲ့ခြောက်ရက်ရယ် လက်ရှိရောက်နေတဲ့ visited_on အတွင်းရှိရမယ်ဆိုပြီး self join နဲ့ join လိုက်ပါတယ်။ ဒါဆိုရင်တော့ 7 ရက်အတွင်းရှိတဲ့ record တွေပါလာမှာဖြစ်ပေမဲ့ 7 ရက်ပြည့်မှစတွက်လို့ရမှာဖြစ်တဲ့အတွက် D1 ရဲ့ visited on က 7 ရက်ပြည့်ရမယ်ဆိုပြီးစစ်ထားလိုက်ပါတယ်။ ဒါဆိုရင်တော့ လိုချင်တဲ့ condition နဲ့ကိုက်ညီသွားပြီဗျာ။ အပေါ်မှာတော့ amount က တော့ 7ရက်အတွင်းပေါင်းထားတဲ့ amount ဆိုတော့ d2 ရဲ့ daily amount တွေကိုပေါင်းပေးလိုက်ပါတယ်။ average amount ကတော့ စုစုပေါင်း ပေါင်းလို့ရတာကို 7နဲ့စားပြီး decimal 2 နေရာစာ ဖြတ်ထားပေးလိုက်ပါတယ်။ visited on ကို ASC order စီပေးလိုက်ပါတယ်ဗျာ။
ဒါဆိုရင် လိုချင်တဲ့အဖြေလေးရရှိသွားပြီပဲဖြစ်ပါတယ်ဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 25 - 1321. Restaurant Growth
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


