Thuta Learning
SQL
AdvancedData & Databasesအခြေခံ

Mini Project: Customer Order Report

SQLသင်ခန်းစာ 2740 မိနစ်

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

  • Primary key, foreign key, CHECK ပါတဲ့ table နှစ်ခုနဲ့ sample data ကို script တစ်ခုထဲ ရေးနိုင်ဖို့
  • LEFT JOIN + COUNT(o.order_id) နဲ့ order မရှိတဲ့ customer တွေကို 0 အဖြစ် ပြနိုင်ဖို့
  • Date filter ကို WHERE မှာမဟုတ်ဘဲ ON clause မှာ ထည့်ရတဲ့ အကြောင်းရင်းကို ရှင်းပြနိုင်ဖို့
  • COALESCE(SUM(...), 0) နဲ့ COUNT(DISTINCT ...) ကို report မှာ မှန်မှန်ကန်ကန် သုံးနိုင်ဖို့

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

ဒီ project မှာ ဘာဆောက်မလဲ

Online ဆိုင်တစ်ဆိုင်ရဲ့ sales team က 'customer တစ်ယောက်ချင်း order ဘယ်နှခု မှာထားပြီးပြီလဲ၊ ဘယ်သူတွေ တစ်ခါမှ မမှာရသေးလဲ' ဆိုတာကို နေ့တိုင်း သိချင်ပါတယ်။ ဒီ project မှာ အဲဒီ question ကို ဖြေတဲ့ report query ကို အခြေခံကနေ စဆောက်ပါမယ်။

အဆင့် သုံးဆင့် ရှိပါတယ်: ပထမ table နှစ်ခုနဲ့ sample data ကို ဆောက်၊ ဒုတိယ customer တစ်ယောက်ချင်းရဲ့ order အရေအတွက်ကို report လုပ်၊ တတိယ ဝင်ငွေနဲ့ ရက်စွဲ filter ကို ထည့်ပါမယ်။ အဆင့်တိုင်းမှာ ရလဒ်ကို ကိုယ့်ခေါင်းထဲ အရင်ခန့်မှန်းပြီးမှ run ကြည့်ပါ၊ မတူရင် ဘာကြောင့်လဲ ရှာပါ။

Problem → Why → Solution

ပြဿနာ: report ထဲက customer တွေ ပျောက်နေတယ်

ပထမ version ကို ရေးတဲ့ developer က JOIN (INNER JOIN) ကို သုံးလိုက်ပါတယ်။ Report မှာ Ana နဲ့ Antonio ပဲ ပေါ်ပြီး Alfreds နဲ့ Around the Horn လုံးဝ မပါပါဘူး။ ဒါပေမဲ့ sales team က ဆက်သွယ်ချင်တာ အဲဒီ order မရှိသေးတဲ့ customer တွေပါပဲ။ Query က error မပေးလို့ ဘယ်သူမှ မသိလိုက်ဘဲ လနှစ်လလောက် report မှားနေခဲ့ပါတယ်။

report-inner-join.sql
SELECT c.customer_name, COUNT(*) AS total_orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.customer_id   -- INNER JOIN
GROUP BY c.customer_id, c.customer_name;

-- only customers WITH orders appear; the zero-order ones are missing

ဘာကြောင့် ဒီလို ဖြစ်တာလဲ

INNER JOIN က table နှစ်ခုလုံးမှာ ကိုက်ညီတဲ့ row ရှိမှသာ ရလဒ် row ထုတ်ပေးပါတယ်။ Order မရှိတဲ့ customer က join ပြီးကတည်းက ပျောက်သွားလို့ GROUP BY ကို ဘယ်တော့မှ မရောက်ပါဘူး။ LEFT JOIN က ဘယ်ဘက် table (customers) ရဲ့ row တိုင်းကို ထိန်းထားပြီး ကိုက်ညီတဲ့ order မရှိရင် order column တွေကို NULL နဲ့ ဖြည့်ပေးပါတယ်။

Core concept: report တစ်ခုကို အဆင့်လိုက် စဉ်းစားခြင်း

Report က ယုံကြည်ရလောက်အောင် မှန်ဖို့ data ကိုယ်တိုင်က မှန်ရပါမယ်၊ ဒါကြောင့် schema ကနေ စပါတယ်။ customers မှာ customer_id primary key ရှိလို့ customer တစ်ယောက်ကို တစ်ကြောင်းတည်းနဲ့ ခွဲခြားနိုင်ပါတယ်။ orders.customer_id က NOT NULL နဲ့ foreign key ဖြစ်လို့ ပိုင်ရှင်မရှိတဲ့ order မရှိနိုင်ပါဘူး။ amount ပေါ်က CHECK (amount > 0) က ဝင်ငွေ စုစုပေါင်းကို အနုတ် value တွေ မဖျက်ဆီးအောင် ကာကွယ်ပါတယ်။ ငွေပမာဏကို float မဟုတ်ဘဲ numeric(10, 2) နဲ့ သိမ်းတာကလည်း ပေါင်းတဲ့အခါ ဒသမ အမှား မဖြစ်အောင်ပါ။

Report query တွေကို မှန်ကန်အောင် ရေးဖို့ PostgreSQL က ဘယ်အစီအစဉ်နဲ့ အလုပ်လုပ်လဲ သိဖို့ လိုပါတယ်။ ရေးတဲ့ အစီအစဉ်က SELECT နဲ့ စပေမဲ့ logical အစီအစဉ်ကတော့ FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY ဖြစ်ပါတယ်။ Step တစ်ခုချင်းစီက ရှေ့ step ရဲ့ ရလဒ်ကိုပဲ မြင်ရပါတယ်။

LEFT JOIN: customer တိုင်းကို ထိန်းထား

Ana အတွက် order နှစ်ခုရှိလို့ row နှစ်ကြောင်း၊ Antonio အတွက် တစ်ကြောင်း ထွက်ပါတယ်။ Alfreds နဲ့ Around the Horn ကတော့ order_id NULL ပါတဲ့ row တစ်ကြောင်းစီ ရပါတယ်။

GROUP BY: customer တစ်ယောက် တစ်ကြောင်း

customer_id နဲ့ group လုပ်တာ အရေးကြီးပါတယ်၊ နာမည်တူ customer နှစ်ယောက် ရှိနိုင်လို့ပါ။ customer_id က primary key ဖြစ်လို့ PostgreSQL က customer_name နဲ့ country ကိုလည်း SELECT ထဲ ထည့်ခွင့်ပေးပါတယ်။

COUNT(o.order_id): NULL ကို မရေတွက်

COUNT(*) က row ကို ရေတွက်လို့ order မရှိတဲ့ customer အတွက် 1 ပြပါတယ်။ COUNT(o.order_id) ကတော့ NULL မဟုတ်တဲ့ value တွေကိုပဲ ရေတွက်လို့ မှန်ကန်တဲ့ 0 ကို ရပါတယ်။

SUM ကလည်း NULL တွေကို ကျော်ပါတယ်၊ ဒါပေမဲ့ group တစ်ခုလုံး NULL ဖြစ်နေရင် 0 မဟုတ်ဘဲ NULL ကို ပြန်ပေးပါတယ်။ Report မှာ 0 ပြချင်ရင် COALESCE(SUM(o.amount), 0) လို့ ရေးရပါတယ်။ ORDER BY မှာ total_orders DESC တစ်ခုတည်း ထားရင် အရေအတွက် တူနေတဲ့ customer တွေရဲ့ အစီအစဉ်ကို PostgreSQL က အာမမခံပါဘူး၊ run တိုင်း ပြောင်းနိုင်ပါတယ်။ ဒါကြောင့် customer_name ကို ဒုတိယ sort key အဖြစ် ထည့်ပါတယ်။

Report pipeline
  1. customers ကို base table အဖြစ် ယူ
  2. LEFT JOIN orders (filter က ON ထဲ)
  3. customer_id နဲ့ GROUP BY
  4. COUNT/SUM, NULL ကို COALESCE
  5. ORDER BY + tie-breaker

Report မှာ 'ဘယ်သူ မပါသင့်လဲ' ထက် 'ဘယ်သူ ပါကို ပါရမလဲ' ကို အရင်မေးပါ၊ အဲဒီ table က LEFT JOIN ရဲ့ ဘယ်ဘက်မှာ ရှိရပါမယ်။

အထွေထွေ ပုံစံ

sql
SELECT parent.key_column,
       parent.label_column,
       COUNT(child.id_column)           AS child_count,
       COALESCE(SUM(child.amount), 0)   AS child_total
FROM parent_table AS parent
LEFT JOIN child_table AS child
       ON child.parent_key = parent.key_column
      AND child_filter_condition
GROUP BY parent.key_column, parent.label_column
ORDER BY child_count DESC, parent.label_column;

Parent table က အမြဲ ပါရမယ့် row တွေကို ပေးပြီး child table က ရေတွက်/ပေါင်းမယ့် row တွေကို ပေးပါတယ်။ Child ပေါ်က filter ကို ON ထဲမှာ ထားမှ parent row တွေ မပျောက်ပါဘူး။

Worked example: schema, data နဲ့ ပထမ report

အောက်က script တစ်ခုတည်းက project တစ်ခုလုံးရဲ့ အခြေခံ ဖြစ်ပါတယ်။ Table နှစ်ခု ဆောက်၊ customer လေးယောက်နဲ့ order သုံးခု ထည့်ပြီး report query ကို run ပါတယ်။ Script ကို empty PostgreSQL database ပေါ်မှာ အစကနေ အဆုံးအထိ run လို့ ရပါတယ်။ Report မ run ခင် ရလဒ်ကို ခန့်မှန်းကြည့်ပါ: Ana က order ဘယ်နှခု ရှိမလဲ?

customer-report.sql
CREATE TABLE customers (
    customer_id   integer PRIMARY KEY,
    customer_name text NOT NULL,
    country       text NOT NULL
);

CREATE TABLE orders (
    order_id    integer PRIMARY KEY,
    customer_id integer NOT NULL REFERENCES customers (customer_id),
    order_date  date NOT NULL,
    amount      numeric(10, 2) NOT NULL CHECK (amount > 0)
);

INSERT INTO customers VALUES
    (1, 'Alfreds Futterkiste', 'Germany'),
    (2, 'Ana Trujillo', 'Mexico'),
    (3, 'Antonio Moreno', 'Mexico'),
    (4, 'Around the Horn', 'UK');

INSERT INTO orders VALUES
    (10, 2, '2026-01-15', 120.00),
    (11, 3, '2026-02-03', 45.50),
    (12, 2, '2026-03-08', 35.00);

SELECT c.customer_name,
       c.country,
       COUNT(o.order_id) AS total_orders
FROM customers AS c
LEFT JOIN orders AS o
       ON o.customer_id = c.customer_id
GROUP BY c.customer_id, c.customer_name, c.country
ORDER BY total_orders DESC, c.customer_name;

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Rules protect the reportcustomer_id integer NOT NULL REFERENCES customers (customer_id),

    Foreign key ကြောင့် မရှိတဲ့ customer ရဲ့ order ဝင်လို့ မရပါဘူး။ ဒါကြောင့် report ထဲက order တိုင်း customer တစ်ယောက်နဲ့ ချိတ်ပြီးသားပါ။

  2. Keep every customerLEFT JOIN orders AS o

    customers ကို ဘယ်ဘက်မှာ ထားလို့ order မရှိတဲ့ Alfreds နဲ့ Around the Horn လည်း NULL order နဲ့ ကျန်ပါတယ်။

  3. Count real orders onlyCOUNT(o.order_id) AS total_orders

    NULL order_id ကို မရေတွက်လို့ order မရှိတဲ့ customer တွေ 0 ရပါတယ်။ COUNT(*) ဆိုရင် 1 ရပါမယ်။

  4. Group by the keyGROUP BY c.customer_id, c.customer_name, c.country

    customer_id က customer တစ်ယောက်ကို တိကျစွာ ခွဲခြားပါတယ်။ နာမည်နဲ့ country ကို ထည့်ထားတာက standard SQL နဲ့ database တိုင်းမှာ အလုပ်လုပ်အောင်ပါ။

  5. Deterministic orderORDER BY total_orders DESC, c.customer_name;

    Order အများဆုံး customer ကို ထိပ်ဆုံးမှာ ထားပြီး အရေအတွက် တူရင် နာမည်နဲ့ စီပါတယ်၊ ဒါကြောင့် run တိုင်း ရလဒ် တူတူ ရပါတယ်။

You should see
customer_name       | country | total_orders
--------------------+---------+-------------
Ana Trujillo        | Mexico  |            2
Antonio Moreno      | Mexico  |            1
Alfreds Futterkiste | Germany |            0
Around the Horn     | UK      |            0

အဆင့် ၃: ဝင်ငွေနဲ့ ရက်စွဲ filter

Sales team က ဖေဖော်ဝါရီ ၁ ရက်နေ့ကစပြီး order အရေအတွက်နဲ့ ဝင်ငွေကို လိုချင်ပါတယ်၊ ဒါပေမဲ့ customer လေးယောက်လုံး ဆက်ပေါ်ရပါမယ်။ Date condition ကို ON clause ထဲ ထည့်လိုက်တော့ join က ဇန်နဝါရီ order ကိုပဲ ကျော်ပြီး customer row တွေကို မထိပါဘူး။ ရလဒ်က Antonio (1, 45.50), Ana (1, 35.00), Alfreds (0, 0), Around the Horn (0, 0) ဖြစ်ပါတယ်။

report-since-feb.sql
SELECT c.customer_name,
       COUNT(o.order_id)          AS orders_since_feb,
       COALESCE(SUM(o.amount), 0) AS revenue_since_feb
FROM customers AS c
LEFT JOIN orders AS o
       ON o.customer_id = c.customer_id
      AND o.order_date >= DATE '2026-02-01'
GROUP BY c.customer_id, c.customer_name
ORDER BY revenue_since_feb DESC, c.customer_name;

WHERE ထဲ ရွှေ့လိုက်ရင်

WHERE o.order_date >= '2026-02-01' လို့ ရေးရင် WHERE က join ပြီးမှ run တာမို့ order မရှိတဲ့ row တွေရဲ့ order_date NULL က condition ကို မအောင်ပါဘူး။ Alfreds နဲ့ Around the Horn ပျောက်သွားပြီး ဇန်နဝါရီ order တစ်ခုပဲ ရှိတဲ့ customer တွေလည်း ပျောက်ပါတယ်။ LEFT JOIN က INNER JOIN အဖြစ် တိတ်တိတ်လေး ပြောင်းသွားတာပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ ဒီ report

ဒီ pattern — parent table ကို LEFT JOIN နဲ့ child ကို ရေတွက်တာ — က dashboard တွေရဲ့ အခြေခံပါ: product တစ်ခုချင်းရဲ့ review အရေအတွက်၊ teacher တစ်ယောက်ချင်းရဲ့ student အရေအတွက်၊ category တစ်ခုချင်းရဲ့ ရောင်းအား စသဖြင့်ပါ။ Metabase, Grafana, Looker Studio လို BI tool တွေထဲမှာ ဒီ SQL ကိုပဲ ကပ်ပြီး chart ဆွဲလေ့ ရှိပါတယ်။

  • Report ကို VIEW (CREATE VIEW customer_order_report AS ...) အဖြစ် သိမ်းထားရင် dashboard နဲ့ API နှစ်ခုလုံးက logic တစ်ခုတည်းကို မျှသုံးနိုင်ပါတယ်။
  • Row သန်းချီ ရှိလာရင် orders(customer_id, order_date) ပေါ်မှာ index ထည့်ပြီး EXPLAIN ANALYZE နဲ့ plan ကို စစ်ပါတယ်၊ အလွန်ကြီးရင် materialized view နဲ့ ညတိုင်း refresh လုပ်ပါတယ်။
  • Date filter ကို app က ပေးမယ်ဆိုရင် previous lesson က parameterized query ($1) နဲ့ ပို့ပါ၊ date string ကို SQL ထဲ ဆက်မရေးပါနဲ့။

အမှားများ

ဒီ report မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ

LEFT JOIN နဲ့ COUNT(*) ကို တွဲသုံးခြင်း

Order မရှိတဲ့ customer တိုင်းက NULL row တစ်ကြောင်း ရှိနေလို့ COUNT(*) က 1 ကို ပြပါတယ်။ Report မှာ customer တိုင်း order အနည်းဆုံး တစ်ခု ရှိသယောင် ဖြစ်နေပါမယ်။ Child table ရဲ့ NOT NULL column (primary key လို) ကို COUNT ထဲ ထည့်ပါ။

Join ပြီးမှ row တွေ ပွားလာတာကို မေ့ခြင်း

Country အလိုက် customer အရေအတွက်ကို ရေတွက်တဲ့အခါ join ပြီး Ana က row နှစ်ကြောင်း ဖြစ်နေလို့ COUNT(c.customer_id) က Mexico ကို 3 လို့ ပြပါတယ်။ Join ပြီးနောက် တစ်ယောက်ကို တစ်ကြိမ်ပဲ ရေတွက်ချင်ရင် COUNT(DISTINCT c.customer_id) ကို သုံးပါ။

Tie-breaker မပါတဲ့ ORDER BY

ORDER BY total_orders DESC တစ်ခုတည်းဆိုရင် 0 ရတဲ့ customer နှစ်ယောက်ရဲ့ အစီအစဉ်က သတ်မှတ်မထားပါဘူး။ Test တွေ တစ်ခါအောင် တစ်ခါကျ ဖြစ်တတ်ပြီး pagination မှာ row တွေ ထပ်ပေါ် ဒါမှမဟုတ် ပျောက်တတ်ပါတယ်။

Best Practices

Best practices

  • Query မ run ခင် sample data ပေါ်မှာ ရလဒ်ကို လက်နဲ့ အရင်တွက်ပါ၊ ကိုယ်ခန့်မှန်းတာနဲ့ မတူရင် query ထက် ကိုယ့်နားလည်မှုကို အရင်စစ်ပါ။
  • Order မရှိတဲ့ customer၊ order အများကြီးရှိတဲ့ customer၊ filter နယ်နိမိတ်ပေါ်က ရက်စွဲ တို့ကို test data ထဲ တမင်ထည့်ပါ။
  • Table alias (c, o) ကို တိုတိုရှင်းရှင်း ပေးပြီး column တိုင်းကို alias နဲ့ ရေးပါ၊ ဘယ် table က လာလဲ ချက်ချင်း သိရပါတယ်။

Mini challenge

Mini challenge: country summary

Management က country တစ်ခုချင်းအတွက် customer အရေအတွက်၊ order အရေအတွက်နဲ့ စုစုပေါင်း ဝင်ငွေကို တစ်ကြောင်းစီ လိုချင်ပါတယ်။ Order မရှိတဲ့ country တွေလည်း 0 နဲ့ ပေါ်ရပါမယ်။ အပေါ်က schema နဲ့ data ကို သုံးပါ။

  • customers ကို base table ထားပြီး orders ကို LEFT JOIN လုပ်ပါ
  • Customer ကို COUNT(DISTINCT ...) နဲ့ တစ်ယောက် တစ်ကြိမ်ပဲ ရေတွက်ပါ
  • Order မရှိရင် revenue ကို NULL အစား 0 ပြပါ
  • Revenue များရာကနေ စီပြီး တူရင် country နာမည်နဲ့ စီပါ
country-summary.sql
SELECT c.country,
       -- TODO: number of distinct customers
       -- TODO: number of orders (customers without orders count 0)
       -- TODO: total revenue, 0 instead of NULL
FROM customers AS c
-- TODO: join orders so that every customer is kept
-- TODO: one row per country
-- TODO: highest revenue first, ties by country
;

ရလဒ်: Row သုံးကြောင်း: Mexico (customers 2, orders 3, revenue 200.50), Germany (1, 0, 0), UK (1, 0, 0)။

အရိပ်အမြွက် ကြည့်ရန်

Mexico မှာ customer နှစ်ယောက်နဲ့ order သုံးခု ရှိပါတယ်။ COUNT(c.customer_id) က 3 ပြရင် DISTINCT လိုနေပါသေးတယ်။ Revenue အတွက် COALESCE(SUM(o.amount), 0) ကို သုံးပါ။

အဖြေ ကြည့်ရန်
country-summary.sql
SELECT c.country,
       COUNT(DISTINCT c.customer_id) AS customers,
       COUNT(o.order_id)             AS orders,
       COALESCE(SUM(o.amount), 0)    AS revenue
FROM customers AS c
LEFT JOIN orders AS o
       ON o.customer_id = c.customer_id
GROUP BY c.country
ORDER BY revenue DESC, c.country;

Quick Quiz 1 / 3

Report မှာ customers LEFT JOIN orders ပြီး COUNT(*) ကို သုံးထားပါတယ်။ Order မရှိတဲ့ Alfreds အတွက် ဘာပြမလဲ?

Quick Quiz 2 / 3

LEFT JOIN report ကို ဖေဖော်ဝါရီကစတဲ့ order တွေပဲ ရေတွက်ချင်ပြီး customer အားလုံးကို ဆက်ပြချင်ပါတယ်။ Date condition ကို ဘယ်မှာ ထည့်ရမလဲ?

Quick Quiz 3 / 3

Country အလိုက် customer ရေတွက်တဲ့ query မှာ Mexico (customer 2 ယောက်၊ order 3 ခု) အတွက် COUNT(c.customer_id) က ဘာပြမလဲ?

မှတ်ထားရမယ့် အချက်များ

  • Report မှာ အမြဲပါရမယ့် table ကို LEFT JOIN ရဲ့ ဘယ်ဘက်မှာ ထားပါ။
  • COUNT(child.id) က NULL ကို မရေတွက်လို့ 0 ရပြီး COUNT(*) က 1 ပြပါတယ်။
  • Child ပေါ်က filter ကို ON ထဲ ထားပါ၊ WHERE ထဲ ထားရင် LEFT JOIN က INNER JOIN ဖြစ်သွားပါတယ်။
  • COALESCE(SUM(...), 0), COUNT(DISTINCT ...) နဲ့ tie-breaker ပါတဲ့ ORDER BY က report ကို မှန်ကန်ပြီး တည်ငြိမ်စေပါတယ်။

PostgreSQL SQL Tutorial — PostgreSQL Global Development Group

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Mini Project: Customer Order Report | Thuta Learning