Pivot MySQL

Learn via video courses
Topics Covered

What is a pivot query in MySQL?

In databases, it is common to have tables that store different types of data. Sometimes it becomes necessary to transform the row-level data into column-level data. Pivot in MySQL is a type of query that involves transforming rows into columns. It is also known as a crosstab query or a transposed table.

In a pivot query, the rows of the original table are turned into columns, and the columns of the original table become rows. This is useful when you want to summarize data in a table and display it in a different format.

This can be achieved using the PIVOT() function, which is available in some database servers like Oracle or SQL Server. However, MySQL does not support this function. To achieve this in MySQL, a query can be used that makes use of the CASE statement within the SELECT clause to group and summarize the data. The resulting query would then display the rotated data, with the rows converted into columns.

Prerequisites

Before attempting to write a query to pivot table in MySQL, you should have a good understanding of the following concepts:

  • Aggregate functions: You should know how to use functions like COUNT, SUM, AVG, MIN, MAX, etc., to perform calculations on groups of rows in a table.
  • GROUP BY clause: You should know how to use the GROUP BY clause to group rows based on one or more columns in a table.
  • Joins: You should know how to use JOIN clauses to combine data from multiple tables based on common columns.
  • Subqueries: You should be familiar with subqueries and how to use them to generate intermediate results that can be used in a larger query.
  • Conditional expressions: You should be familiar with conditional expressions, such as IF, CASE, and COALESCE, and how to use them to create conditional logic in queries.

Once you have a good understanding of these concepts, you can start to learn about a pivot in MySQL. A pivot query is a way to transform data from rows to columns, or vice versa, based on a particular aggregation function and a set of grouping criteria. Pivot queries can be useful for generating summary reports, analyzing trends, or comparing data across multiple categories.

What is PIVOT?

A pivot table in MySQL is a database operation that allows you to transform data from rows into columns, effectively rotating a table on its axis. In a pivot operation, you select a set of columns that you want to use as new column headers, and then you pivot the data so that the values in those columns become the new column headers, while the remaining columns become the data associated with those headers.

Pivot in MySQL is particularly useful when you have data in a table that needs to be transformed so that you can analyze it more easily. In MySQL, you can use the Pivot operation to transform rows into columns. However, unlike other databases, MySQL doesn't have a built-in Pivot function.

To perform a pivot operation in MySQL, you typically use an SQL query that includes aggregate functions and a GROUP BY clause to group the data based on the columns that you want to use as headers. You then use conditional expressions to specify how to calculate the values for each header column based on the original data.

Sample Data and Expected Output

Pivot in MySQL is a type of query that allows you to transform rows of data into columns, effectively "pivoting" the table. This is useful when you want to summarize data and present it in a more digestible format.

In a pivot query, you specify the columns that you want to pivot on, as well as the aggregate function(s) that you want to apply to the values in those columns. The result of the query is a new table with the pivoted columns as its headers.

Assuming you have a sample table called sales_data with the following columns and data:

Product NameSales DateSales Amount
Product A2022-01-01500
Product B2022-01-01750
Product C2022-01-011000
Product A2022-01-02800
Product B2022-01-02900
Product C2022-01-021200

To pivot this data by Product Name, you can use the following query:

Code:

Output:

The output will be:

Sales DateProduct AProduct BProduct C
2022-01-015007501000
2022-01-028009001200

In this query, the MAX function is used to aggregate the Sales Amount for each Product Name for each Sales Date. The CASE statement is used to conditionally select the Sales Amount based on the Product Name. The GROUP BY statement is used to group the data by Sales Date.

This query will pivot the data so that the Product Name becomes the column headers, the Sales Date becomes the row headers, and the Sales Amount are the data associated with each Product Name and Sales Date combination.

Dynamic Pivot Columns

In MySQL, you can use dynamic pivot columns to create a pivot table with unknown or varying column values. Dynamic pivot columns are useful when you don't know the column values beforehand or when the column values change frequently.

Assuming you have a sample table called sales_data with the following columns and data:

Product NameSales DateSales Amount
Product A2022-01-01500
Product B2022-01-01750
Product C2022-01-011000
Product A2022-01-02800
Product B2022-01-02900
Product C2022-01-021200
Product D2022-01-021500

To pivot this data by Product Name, you can use dynamic pivot columns with the following query:

Code:

Output:

The output will be:

Sales DateProduct AProduct BProduct CProduct D
2022-01-015007501000NULL
2022-01-0280090012001500

In this query, the first SELECT statement uses GROUP_CONCAT to dynamically generate the pivot columns based on the distinct Product Name values in the sales_data table.

The generated SQL statement is then stored in a user-defined variable @sql. The PREPARE statement is used to prepare the SQL statement in @sql, and the EXECUTE statement is used to execute the prepared statement. Finally, the DEALLOCATE PREPARE statement is used to deallocate the prepared statement.

This query will pivot the data so that Product Name becomes the column header, Sales Date becomes the row header, and the Sales Amount is the data associated with each Product Name and Sales Date combination. The dynamic pivot columns allow for flexibility in column values, making it easy to pivot data with unknown or varying column values.

Rotate Rows to Columns Using CASE Statement

It's not possible to rotate rows to columns using a CASE statement alone, as the CASE statement is used for conditional expressions and doesn't provide the necessary functionality for pivoting data. To rotate rows to columns, you can use a combination of aggregate functions and the GROUP BY clause along with the CASE statement.

Suppose you have a table named sales_data that contains data about sales transactions, with columns transaction_id, product_name, and sales_amount. Here's some sample data:

transaction_idproduct_namesales_amount
1Product A100
2Product B200
3Product A150
4Product C50
5Product B300

Suppose you want to pivot the product_name column into separate columns so that the output shows the total sales amount for each product. Here's the code you can use:

Code:

Output:

In this query, the CASE statements are used to identify the sales amounts that need to be pivoted into columns for each product, and the aggregate function SUM is used to calculate the total sales amount for each product. The output of this query would be:

Product_A_SalesProduct_B_SalesProduct_C_Sales
25050050

This output shows that Product A had total sales of 250, Product B had total sales of 500, and Product C had total sales of 50.

Rotate Rows to Columns Using CASE and SUM()

While it's not possible to rotate rows to columns using just the CASE statement, you can use the SUM() function in combination with the CASE statement to achieve this pivot.

Here's an example code snippet that demonstrates how to use the SUM() function and the CASE statement to pivot rows into columns:

Suppose you have a table named customer_orders with columns order_id, customer_name, order_date, and order_amount. Here's some sample data:

order_idcustomer_nameorder_dateorder_amount
1John Smith2022-01-01100
2Mary Johnson2022-01-01200
3John Smith2022-01-02150
4Susan Lee2022-01-0250
5Mary Johnson2022-01-03300

Suppose you want to pivot the order_date column into separate columns so that the output shows the total order amount for each date. Here's the code you can use:

Code:

Output:

In this query, the CASE statements are used to identify the order amounts that need to be pivoted into columns for each date, and the aggregate function SUM is used to calculate the total order amount for each date. The output of this query would be:

customer_name2022-01-012022-01-022022-01-03
John Smith1001500
Mary Johnson2000300
Susan Lee0500

This output shows that John Smith had total orders of 100 on 2022-01-01, 150 on 2022-01-02, and no orders on 2022-01-03, Mary Johnson had total orders of 200 on 2022-01-01, no orders on 2022-01-02, and 300 on 2022-01-03, and Susan Lee had no orders on 2022-01-01 and 2022-01-03, and order of 50 on 2022-01-02.

Rotate Rows to Columns in Multiple Tables

Rotating rows to columns in multiple tables involves joining the tables together and using the CASE statement and aggregate functions such as SUM() or COUNT() to pivot the data. Here's an example:

Suppose you have two tables, customers, and orders.

  • The customers table contains columns customer_id, customer_name, and customer_email.
  • The orders table contains columns order_id, customer_id, order_date, and order_amount.

Customers table:

customer_idcustomer_namecustomer_email
1John Smithjohn@example.com
2Mary Johnsonmary@example.com
3Susan Leesusan@example.com

Orders table:

order_idcustomer_idorder_dateorder_amount
112022-01-01100
222022-01-01200
312022-01-02150
432022-01-0250
522022-01-03300

Suppose you want to pivot the order_date column into separate columns for each customer so that the output shows the total order amount for each customer on each date. Here's the code you can use:

Code:

Output:

The JOIN keyword is first used to combine rows from the customers and orders tables based on the common column customer_id to combine data from both tables.

Then, the CASE statements are used to identify the order amounts that need to be pivoted into columns for each date, and the aggregate function SUM is used to calculate the total order amount for each date. The output of this query would be:

customer_name2022-01-012022-01-022022-01-03
John Smith1001500
Mary Johnson2000300
Susan Lee0500

This output shows that John Smith had total orders of 100 on 2022-01-01, 150 on 2022-01-02, and no orders on 2022-01-03, Mary Johnson had total orders of 200 on 2022-01-01, no orders on 2022-01-02, and 300 on 2022-01-03, and Susan Lee had no orders on 2022-01-01 and 2022-01-03, and order of 50 on 2022-01-02.

Conclusion

Here are some key points to remember about pivoting in MySQL:

  • Pivot in MySQL transforms rows into columns in a table, using aggregate functions such as SUM(), COUNT(), or AVG().
  • Pivot table in MySQL can be achieved using the CASE statement in combination with aggregate functions.
  • Pivot is useful for summarizing data and creating reports.
  • Pivot can be done on a single table or multiple tables, by joining the tables together.
  • Pivot table in MySQL can be used to transform data into a more readable and actionable format, and can help identify patterns and trends that might not be easily visible in a tabular format.
  • Pivot in MySQL is a common technique used in business intelligence and data analytics.