Skip to main content
Log inGet a demo
Back to SQL Dictionary
Advanced

SQL Pivoting Data

What is SQL Pivoting Data?

SQL pivoting is a technique used to restructure data from a row-based format into a column-based format. It involves transforming data from a tall and narrow shape to a wide shape, often making it more accessible for analysis and reporting. Pivoting is especially useful when you want to summarize or aggregate data for reporting or analysis.

When you would use it

You would use SQL pivoting when you want to:

  1. Create summary reports: Pivoting allows you to summarize data by categories or dimensions to create summary reports, such as sales by month, product categories, or regions.

  2. Change the data representation: Pivoting can help change the representation of data, making it more convenient for analysis or visualization.

  3. Convert normalized data: If data is stored in a normalized form with separate rows for different attributes, pivoting can transform it into a denormalized structure for easier querying and reporting.

  4. Transpose data: If data is stored in a format where you want to switch rows to columns and vice versa, pivoting is useful.

  5. Aggregate data: Pivoting is commonly used to aggregate and summarize data, such as calculating totals, averages, or counts for various categories.

Syntax

SQL does not have a single standard pivot operation. The syntax and approach for pivoting data can vary depending on the specific database management system (DBMS) you are using. However, the general idea is to use an aggregate function like SUM, MAX, or AVG along with a CASE statement to conditionally transform data.

Here's a simplified template:

SELECT
    [category],
    SUM(CASE WHEN [condition] THEN [value] ELSE 0 END) AS [new_column_name]
FROM [table]
GROUP BY [category];
  • [category]: The column used to define categories or dimensions for pivoting.
  • [condition]: The condition that determines which values to include in the aggregation.
  • [value]: The column containing the values to be aggregated.
  • [new_column_name]: The name of the new pivoted column.

Parameter values

  • [category]: The column that defines categories or dimensions.
  • [condition]: The condition used to filter or categorize data.
  • [value]: The column containing the data to be aggregated.
  • [new_column_name]: The name of the new pivoted column.

Example query

Suppose you have a table "sales" with columns "product_category," "sales_date," and "revenue," and you want to pivot the data to show total revenue for each product category by year. You can use a query like this:

SELECT
    product_category,
    SUM(CASE WHEN EXTRACT(YEAR FROM sales_date) = 2022 THEN revenue ELSE 0 END) AS revenue_2022,
    SUM(CASE WHEN EXTRACT(YEAR FROM sales_date) = 2023 THEN revenue ELSE 0 END) AS revenue_2023
FROM sales
GROUP BY product_category;

Example table response

Assuming the "sales" table contains data like this:

| product_category | sales_date  | revenue |
| ---------------  | ----------- | ------- |
| Electronics      | 2022-05-10  | 1200    |
| Clothing         | 2022-06-15  | 800     |
| Electronics      | 2023-02-20  | 1500    |
| Clothing         | 2023-03-05  | 1000    |

The query mentioned earlier would return the following result:

| product_category | revenue_2022 | revenue_2023 |
| ---------------  | -----------   | -----------   |
| Electronics      | 1200         | 1500         |
| Clothing         | 800          | 1000         |

This result shows the total revenue for each product category in the years 2022 and 2023.

Use cases

  • Generating summary reports and dashboards.
  • Transposing data from a tall to a wide format for easier analysis.
  • Converting normalized data into a denormalized form for reporting.
  • Aggregating data by categories or dimensions.

SQL languages this is available for

SQL pivoting is available in most relational database management systems (RDBMS), including but not limited to:

  • SQL Server
  • Oracle Database
  • MySQL
  • PostgreSQL
  • SQLite
  • IBM Db2
  • MariaDB

The specific syntax and functions for pivoting may vary among DBMSs, so it's essential to refer to the documentation for the DBMS you are using for precise details.

Related

SQL Window Functions

SQL Row Number Over Partition By

SQL Relative Dates

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes