Thuta Learning
ရှာဖွေရန်
AdvancedData & Databasesbeginner

GROUP BY and HAVING

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • GROUP BY query ရေးရန်
  • WHERE နှင့် HAVING ကိုခွဲခြားရန်
  • COUNT/SUM/AVG သုံးရန်

ရိုးရိုးလေး ရှင်းပြမယ်

GROUP BY သည် တူညီသော column value ရှိသည့် rows များကိုအုပ်စုဖွဲ့သည်။ WHERE က grouping မလုပ်ခင် row ကိုစစ်ပြီး HAVING က grouping လုပ်ပြီးနောက် aggregate result ကိုစစ်သည်။

sql
SELECT department,
       COUNT(*) AS employee_count,
       ROUND(AVG(salary), 2) AS average_salary
FROM employees
WHERE active = TRUE
GROUP BY department
HAVING COUNT(*) >= 2
ORDER BY average_salary DESC;
You should see
department | employee_count | average_salary
Engineering | 4 | 1850000.00
Support | 2 | 950000.00

လက်တွေ့စမ်းကြည့်ရန်

orders table မှ customer တစ်ဦးစီ၏ total amount ကိုတွက်ပြီး စုစုပေါင်း 100000 ကျော်သူများသာပြပါ။

Aggregate Functions TutorialPostgreSQL

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Aggregate condition ကို WHERE ထဲရေးခြင်း
  • SELECT ထဲရှိ non-aggregate column ကို GROUP BY ထဲမထည့်ခြင်း

လေ့ကျင့်ခန်း

orders table မှ customer တစ်ဦးစီ၏ total amount ကိုတွက်ပြီး စုစုပေါင်း 100000 ကျော်သူများသာပြပါ။

You'll know it worked when: department | employee_count | average_salary Engineering | 4 | 1850000.00 Support | 2 | 950000.00

GROUP BY and HAVING | Thuta Learning