🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Databases Advanced

What is Crosstab Query?

A query that transforms rows into columns, creating a pivot table view of aggregated data.

Crosstab (pivot) queries rotate data from rows to columns. In PostgreSQL, the tablefunc extension provides the crosstab() function. This is useful for reporting — turning monthly sales rows into columns for each month.

Alternative approaches include CASE expressions with GROUP BY for manual pivoting. Crosstab queries simplify dashboard reporting, time-series analysis, and comparative data views.