0Pricing
PostgreSQL Performance & Query Optimization · บทเรียน

การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข

เรียนรู้ว่าข้อกำหนด FILTER และการรวมแบบมีเงื่อนไขที่อาศัย CASE ช่วยคำนวณตัวชี้วัดหลายรายการในการอ่านตารางเพียงครั้งเดียว แทนการเรียกใช้คำสืบค้นแยกกันหลายครั้งได้อย่างไร

การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Problem: Many Counts, One Table

Dashboards often need several metrics from the same table — total orders, paid orders, refunded orders. Running three separate queries scans the table three times. We can do it in one pass.

Conditional Aggregation with CASE

The classic trick wraps a CASE inside an aggregate. Rows that do not match contribute NULL, which COUNT and SUM ignore.

SELECT
  COUNT(*) AS total,
  COUNT(CASE WHEN status = 'paid' THEN 1 END) AS paid
FROM orders;

The Cleaner FILTER Clause

PostgreSQL offers a more readable form: the FILTER clause attached to any aggregate. It expresses intent directly.

SELECT
  COUNT(*) AS total,
  COUNT(*) FILTER (WHERE status = 'paid')     AS paid,
  COUNT(*) FILTER (WHERE status = 'refunded') AS refunded
FROM orders;

Why This Is Faster

All metrics are computed in a single scan of the table. The planner reads each row once and updates every aggregate, instead of scanning the table separately for each metric.

FILTER with SUM and AVG

FILTER works with any aggregate, not just COUNT. Compute conditional sums and averages in the same query.

SELECT
  SUM(total) FILTER (WHERE status = 'paid') AS revenue,
  AVG(total) FILTER (WHERE status = 'paid') AS avg_paid
FROM orders;

Combining with GROUP BY

FILTER shines inside grouped queries, producing a pivot-like result with one row per group and several conditional columns.

SELECT
  region,
  COUNT(*) FILTER (WHERE status = 'paid')     AS paid,
  COUNT(*) FILTER (WHERE status = 'refunded') AS refunded
FROM orders
GROUP BY region;

Pivoting Months into Columns

A common report turns rows into columns. FILTER makes a clean monthly pivot without extension functions.

SELECT
  product_id,
  SUM(total) FILTER (WHERE month = 1) AS jan,
  SUM(total) FILTER (WHERE month = 2) AS feb
FROM sales
GROUP BY product_id;

Reading the Plan

EXPLAIN ANALYZE confirms a single Aggregate node over one scan. Compare it against three separate queries to see the saved scans.

EXPLAIN ANALYZE
SELECT
  COUNT(*) FILTER (WHERE status = 'paid') AS paid,
  COUNT(*) FILTER (WHERE status = 'refunded') AS refunded
FROM orders;

FILTER vs WHERE

Remember the difference:

  • WHERE removes rows before any aggregate sees them
  • FILTER keeps all rows but restricts which ones a specific aggregate counts

Use FILTER when different aggregates need different conditions.

Combining with Indexes

If most metrics target a subset (e.g. only recent rows), add a WHERE for the shared condition so an index narrows the scan, then use FILTER for the per-metric splits.

SELECT
  COUNT(*) FILTER (WHERE status = 'paid') AS paid
FROM orders
WHERE created_at >= now() - interval '30 days';

Counting Distinct Conditionally

FILTER also pairs with COUNT(DISTINCT ...), letting you count unique customers per status in one scan instead of several grouped queries.

SELECT
  COUNT(DISTINCT customer_id) FILTER (WHERE status = 'paid') AS paying_customers
FROM orders;

Quick Check

Test your conditional aggregation knowledge.

Recap

You learned conditional aggregation:

  • Compute many metrics in one scan with FILTER or CASE
  • FILTER is more readable and works with any aggregate
  • Combine with GROUP BY for pivot-style reports
  • WHERE removes rows; FILTER restricts a single aggregate
  • Add a shared WHERE so indexes narrow the scan

คำถามที่พบบ่อย

บทเรียน “การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข”

เรียนรู้ว่าข้อกำหนด FILTER และการรวมแบบมีเงื่อนไขที่อาศัย CASE ช่วยคำนวณตัวชี้วัดหลายรายการในการอ่านตารางเพียงครั้งเดียว แทนการเรียกใช้คำสืบค้นแยกกันหลายครั้งได้อย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การปรับแต่งฟังก์ชันรวมและฟังก์ชันหน้าต่าง
  2. CTE แบบเรียกซ้ำและคำสั่งค้นหากราฟ
  3. การใช้มุมมองแบบจัดเก็บเพื่อเพิ่มประสิทธิภาพ
  4. การปรับการสืบค้นด้วย FILTER และการรวมแบบมีเงื่อนไข
← กลับไปที่ PostgreSQL Performance & Query Optimization