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.
Count a Particular Value Across All Columns in SQL Server
To count a particular value across all columns of a table, use a CASE sum, CROSS APPLY with VALUES, UNPIVOT or UNION ALL. This post tests all four on a staff contact table, compares their reads on 200,000 rows, and builds the column list with dynamic SQL.
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.







