This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
pivotunpivot

SQL SERVER – Exploring PIVOT and UNPIVOT

October 24, 2023
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL Performance

The SQL Server PIVOT and UNPIVOT operators are powerful tools that provide an easy way to transform your data in SQL.

Read More
A gardener walking a row of berry bushes with three small baskets on one strap, sorting berries in a single pass.

Conditional Aggregation: Several Counts in One Pass With CASE

June 24, 2021
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL CASE, SQL Group By, SQL Scripts, SQL Server

Use conditional aggregation for several ticket counts, avoid COUNT with ELSE zero, and calculate percentages with an explicit denominator.

Read More
Hands sorting loose screws and nuts into a muffin tin, one cup empty and one bent screw set apart

Turning Key-Value Pairs Into Columns for Reporting

September 19, 2013
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL Reports, SQL Scripts, SQL Server

Turn key-value pairs into reporting columns with conditional aggregation and PIVOT, handle missing values and conversions.

Read More
Folded deckchairs taken from a tall stack and opened one by one in a long row along the beach.

Unpivoting Columns Into Rows With CROSS APPLY and VALUES

December 2, 2010
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL NULL, SQL Scripts, SQL Server

Use CROSS APPLY and VALUES for unpivoting columns, retain nulls deliberately, preserve labels, and turn paired fields into aligned rows.

Read More
Six empty ceramic cups in two rows, with terracotta, blue and pale glazes.

CROSS APPLY VALUES: Unpivot Fixed Columns and Preserve NULLs

July 3, 2009
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL Joins, SQL Scripts, SQL Server

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.

Read More
A full spice rack with no room left, a new jar of red paprika standing on the counter below

Dynamic PIVOT: Building Report Columns From the Data

May 17, 2009
Pinal Dave
SQL Tips and Tricks
Dynamic SQL, PIVOT and UNPIVOT, SQL Reports, SQL Server

Build a dynamic PIVOT with ordered STRING_AGG and QUOTENAME, parameterized filters, zero-filled results, and explicit report-width limits.

Read More
A rich painting pairs handmade blue, ochre and sage tiles arranged in horizontal rows and vertical columns.

PIVOT and UNPIVOT: Aggregation, NULLs and Cross-Tab Queries

October 1, 2008
Pinal Dave
SQL Tips and Tricks
PIVOT and UNPIVOT, SQL Group By, SQL Server

PIVOT and UNPIVOT reshape SQL Server data. See a tested example, disappearing NULLs, aggregation and fixed cross-tab columns.

Read More
1 2 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com