In SQL Server (T-SQL) and across relational engines in general, conditional aggregation (SUM(CASE WHEN … THEN … ELSE 0 END) or MAX(CASE WHEN … THEN … END)) is almost always the best, most flexible, and most maintainable choice for converting rows into columns—and often matches or beats the native T-SQL PIVOT operator in execution cost because both typically compile to a single aggregation pass over the input stream.
Use native T-SQL PIVOT only when you want concise syntax for a simple, fixed set of pivoted values and are careful about what columns enter the input subquery.
- Conditional Aggregation (CASE + Aggregate) vs. PIVOT Aspect Conditional Aggregation (CASE + SUM/MAX/COUNT) T-SQL PIVOT Operator Execution efficiency Typically single Stream Aggregate or Hash Match (Aggregate) pass over source data Also compiles to a Hash Match (Aggregate) / Stream Aggregate pass; similar cost on clean inputs Input control Full control over which columns are grouped and aggregated Pivots all remaining unreferenced columns from the input subquery/table—extra columns can accidentally split groups Multi-metric / custom logic Easy to combine multiple conditions, NULLs, or different aggregates (SUM, COUNT, MAX) in one pass Limited to one aggregate function and fixed value list per PIVOT clause Portability Works across SQL Server, MySQL, PostgreSQL, Oracle, SQLite SQL Server / Oracle / Snowflake-specific syntax
- Recommended Pattern: Conditional Aggregation in T-SQL Pre-aggregate or filter first using SARGable predicates (such as a date range), then pivot status or category values into columns in a single pass:
SQL SELECT customer_id, SUM(CASE WHEN status = ‘COMPLETED’ THEN total_amount ELSE 0.00 END) AS completed_amount, SUM(CASE WHEN status = ‘PENDING’ THEN total_amount ELSE 0.00 END) AS pending_amount, SUM(CASE WHEN status = ‘CANCELLED’ THEN 1 ELSE 0 END) AS cancelled_count, COUNT(*) AS total_orders FROM dbo.orders WHERE order_date >= ‘2026-01-01’ AND order_date < ‘2027-01-01’ GROUP BY customer_id ORDER BY customer_id; Why pre-filtering helps: Filtering order_date with a half-open range (>= and <) lets SQL Server seek an index on order_date or customer_id before aggregating.
Why ELSE 0.00 (or ELSE 0) matters for SUM: SUM ignores NULLs, and returning 0.00 instead of NULL keeps numeric reporting clean—just ensure THEN and ELSE share a consistent DECIMAL scale/precision.
- Native T-SQL PIVOT Syntax and Common Trap When using PIVOT, always feed it a narrow derived table or CTE containing only the grouping column(s), the pivoting column, and the value column. If an extra column (like order_id or updated_at) is left in the input to PIVOT, SQL Server includes it in the implicit GROUP BY and produces one row per order_id instead of one row per customer_id.
SQL WITH order_source AS ( SELECT customer_id, status, total_amount FROM dbo.orders WHERE order_date >= ‘2026-01-01’ AND order_date < ‘2027-01-01’ ) SELECT customer_id, ISNULL([COMPLETED], 0.00) AS completed_amount, ISNULL([PENDING], 0.00) AS pending_amount, ISNULL([CANCELLED], 0.00) AS cancelled_amount FROM order_source PIVOT ( SUM(total_amount) FOR status IN ([COMPLETED], [PENDING], [CANCELLED]) ) AS pvt ORDER BY customer_id; 4. Operational Checklist Prefer conditional aggregation (SUM(CASE WHEN … END) / MAX(CASE WHEN … END)) for clarity, multi-condition expressions, and cross-database portability.
Feed PIVOT a narrow CTE/subquery containing only the columns you want grouped, pivoted, and aggregated—never pivot directly over a wide table with unreferenced columns.
Filter before pivoting using SARGable predicates so fewer rows enter the aggregation/sort/hash operators.
Normalize NULLs with ISNULL() / COALESCE() on pivoted output columns when 0 is expected instead of NULL.
