အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 4 အတွက်ကတော့ Leetcode ရဲ့ difficulties medium problem တစ်ပုဒ်ဖြစ်တဲ့ 178. Rank Scores ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ Score ဆိုတဲ့ table လေးတစ်ခုပဲပေးထားပါတယ်။ လိုချင်တာကတော့ score များရာကနေ order စီပေးပြီး score အလိုက် Rank ခွဲပေးရမှာပါ။ score တူနေရင် Rank ကလည်းတူတူပဲဖြစ်ရမယ်ဆိုပြီးပြောထားပါတယ်။
အဲ့တော့ SQL လေးရေးလိုက်ကြရအောင်ဗျာ။
1SELECT s.score, (
2 SELECT COUNT(DISTINCT ssub.score) FROM Scores ssub where ssub.score >= s.score
3) AS 'rank' FROM Scores s ORDER BY s.score DESC;ဒီမှာဆို လိုချင်တဲ့ output အတိုင်း score နဲ့ rank ကို ဆွဲထုတ်ပြီး order ကို desc စီပေးထားပါတယ်။ ဒီနေရာမှာ rank ကို '' ထဲထားရတာက mysql မှာ rank ဆိုတဲ့ keyword ပါနေတော့ မထည့်ထားရင် error တက်နေမှာပဲဖြစ်ပါတယ်။ rank အနေနဲ့ကတော့ ကျွန်တော် sub query လေးရေးထားပါတယ်။ Count function နဲ့ Distinct keyword လေးကိုလည်းသုံးထားပါတယ်။ ရည်ရွယ်ချက်ကတော့ လက်ရှိရောက်နေတဲ့ score ရယ်သူ့ထက်မြင့်တာတွေရယ်ကို rank အနေနဲ့ယူချင်လို့ပါ။
ဆိုကြပါစို့ ကျွန်တော်တို့ score table မှာ score (4,4,3,2,2) ရှိတယ်ဆိုပါစို့။
ပထမဦးဆုံးအနေနဲ့က 4 query အရဆို 4နဲ့ လေးထက်ကြီးတာ ရဲ့ count အရေအတွက်ဖြစ်ရမယ်။ table ထဲမှာ တူညီတဲ့ 4ကနှစ်ခုပါနေတော့ Distinct မထားထားရင် rank က2ဖြစ်နေမှာပါ။ Distinct ထည့်ထားတော့ 4 နဲ့ သူ့အထက်ဆိုတော့ လေးထက်ကြီးတာမရှိတော့ rank က 1ပဲဖြစ်နေမှာပါ။ 3ဆိုလည်း 3နဲ့ 3ထက်ကြီးတာဆိုတေယ့ 3နဲ့4 နှစ်ခုရှိတော့ rank က 2 ဖြစ်နေမှာပါ။ 2 အတွက်ဆို rank က 3ပေါ့။ ဒါဆိုကျွန်တော်တို့လိုချင်တဲ့အတိုင်း output လေးရရှိလာမှာပဲဖြစ်ပါတယ်။ Day4 အတွက်လည်းပြီးဆုံးသွားပြီဆိုတော့ ကိုဖြေထားတဲ့ မတူညီတဲ့ပုံစံလေးတွေရှိရင်လည်း comment မှာပြောပြသွားလို့ရပါတယ်ဗျ။ Video လေးကိုတော့ အောက်က YouTube link လေးကနေဝင်ကြည့်လို့ရပါတယ်ဗျာ။ see you all ပါဗျာ။
30 Days Of LeetCode Database Challenge Day 4 - 178. Rank Scores
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 ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


