The SQL Server PIVOT and UNPIVOT operators are powerful tools that provide an easy way to transform your data in SQL.
Conditional Aggregation: Several Counts in One Pass With CASE
Use conditional aggregation for several ticket counts, avoid COUNT with ELSE zero, and calculate percentages with an explicit denominator.
Turning Key-Value Pairs Into Columns for Reporting
Turn key-value pairs into reporting columns with conditional aggregation and PIVOT, handle missing values and conversions.
Unpivoting Columns Into Rows With CROSS APPLY and VALUES
Use CROSS APPLY and VALUES for unpivoting columns, retain nulls deliberately, preserve labels, and turn paired fields into aligned rows.
CROSS APPLY VALUES: Unpivot Fixed Columns and Preserve NULLs
CROSS APPLY VALUES can turn a fixed set of columns into rows without dropping their NULL values. I make any missing-value filter explicit. That keeps the reshaping step separate from the decision to exclude data.
Dynamic PIVOT: Building Report Columns From the Data
Build a dynamic PIVOT with ordered STRING_AGG and QUOTENAME, parameterized filters, zero-filled results, and explicit report-width limits.
PIVOT and UNPIVOT: Aggregation, NULLs and Cross-Tab Queries
PIVOT and UNPIVOT reshape SQL Server data. See a tested example, disappearing NULLs, aggregation and fixed cross-tab columns.







