အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 23 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 550. Game Play Analysis IV ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ မှာတော့ Activity ဆိုတဲ့ table လေးကိုပေးထားပါတယ်။
1Table: Activity2 3+--------------+---------+4| Column Name | Type |5+--------------+---------+6| player_id | int |7| device_id | int |8| event_date | date |9| games_played | int |10+--------------+---------+11(player_id, event_date) is the primary key (combination of columns with unique values) of this table.12This table shows the activity of players of some games.13Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device.ကျွန်တော်တို့လုပ်ပေးဖို့လိုတာကတော့ ပထမဆုံး loginဝင်ပြီး နောက်ရက်မှာ ထပ်ပြီး login ဝင်တဲ့ user ရဲ့ fraction ကိုရှာပေးရမှာပဲဖြစ်ပါတယ်။ ပထမဆုံး login ဝင်ပြီး နောက်ရက် login ထပ်ဝင်တဲ့ user အရေအတွက်ကို စုစုပေါင်း user အရေအတွက်နဲ့စားပြီး decimal 2 နေရာစာ ROUND လုပ်ပေးရမှာပဲဖြစ်ပါတယ်။
1**Formula**2 3result = ROUND(condition users / total users, 2)ဆိုတော့ formula အရဆို condition နဲ့ကိုက်ညီတဲ့ users အရေအတွက်ကို စုစုပေါင်း users အရေအတွက်နဲ့စားပေးရမှာပဲဖြစ်ပါတယ်။
1Example 1:2 3Input: 4Activity table:5+-----------+-----------+------------+--------------+6| player_id | device_id | event_date | games_played |7+-----------+-----------+------------+--------------+8| 1 | 2 | 2016-03-01 | 5 |9| 1 | 2 | 2016-03-02 | 6 |10| 2 | 3 | 2017-06-25 | 1 |11| 3 | 1 | 2016-03-02 | 0 |12| 3 | 4 | 2018-07-03 | 5 |13+-----------+-----------+------------+--------------+14Output: 15+-----------+16| fraction |17+-----------+18| 0.33 |19+-----------+20Explanation: 21Only the player with id 1 logged back in after the first day he had logged in so the answer is 1/3 = 0.3322 Example မှာဆိုလည်း player 1 ကပဲ ပထမဆုံး login ဝင်ပြီးနောက်ရက်ပြန် login ဝင်ထားတာဖြစ်တော့ condition နဲ့ကိုက်ညီတဲ့ user count က 1ဖြစ်တယ်။ total user count က 3ဆိုတော့ 1/3 ဖြစ်ပြီးရလာတဲ့ result ကို decimal 2 နေရာစာ ROUND လုပ်ပေးတော့ 0.33ဖြစ်သွားတာပဲဖြစ်ပါတယ်။ ကျွန်တော်ကတော့ အောက်ကအတိုင်းလေးရေးဖြစ်ခဲ့ပါတယ်။
1**SQL**2 3SELECT ROUND(4 COUNT(DISTINCT a.player_id) / 5 (SELECT COUNT(DISTINCT player_id) FROM Activity),6 2) AS fraction7FROM Activity a JOIN (8 SELECT player_id, MIN(event_date) AS fd9 FROM Activity GROUP BY player_id10) AS firstday ON firstday.player_id = a.player_id11WHERE a.event_date = DATE_ADD(fd, INTERVAL 1 DAY);12 ဒီ query မှာဆို ပထမဆုံးအနေနဲ့ sub query လေးရေးပြီး ကျွန်တော်တို့ player တစ်ယောက်ချင်းစီရဲ့ ပထမဆုံး login ဝင်ထားတဲ့ record လေးတွေကို ရှာပြီး JOIN ပေးလိုက်ပါတယ်။ ပြီးရင် သူလိုချင်တဲ့ condition နဲ့ကိုက်ညီအောင် Main query ရဲ့ event date က ပထမဆုံး login ဝင်တဲ့ date နဲ့တစ်ရက်ကြီးတာဆိုတော့ ပထမ login date ကို DATE_ADDဆိုတဲ့ function လေးသုံးပြီးစစ်ပေးလိုက်ပါတယ်။ ဒါဆိုရင်သူလို့ချင်တဲ့ condition နဲ့ကိုက်ညီတဲ့ record တွေတော့ရလာခဲ့ပြီ။ အဓိကအပိုင်းလေးကိုပြန်သွားမယ်ဆို ကျွန်တော်တို့က decimal 2 နေရာစာ Round လုပ်ပေးဖို့လိုတော့ formula "(ROUND(condition users / total users, 2))" လေးအတိုင်းရေးလိုက်ပါတယ်။ condition နဲ့ကိုက်ညီတဲ့ user တွေကတော့ ရပြီဖြစ်တဲ့အတွက် COUNT Function လေးနဲ့ ယူလို့ရပါပြီ။ total user count ကတော့ sub query လေးရေးပြီးပြန်ယူလိုက်ပါတယ်။
ဒါဆိုရင် လိုချင်တဲ့အဖြေလေးရရှိသွားပြီပဲဖြစ်ပါတယ်ဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Day 23 - 550. Game Play Analysis IV
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


