Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to get sql pivot multiple columns?

Asked by Ai Oct 3, 2022 4.0K views 3 answers
Share

About this question

 What is the best way to 'flatten' tables into a single row?


For example, with the following table:


+-----+-------+-------------+------------------+
| Id  | hProp | iDayOfMonth | dblTargetPercent |
+-----+-------+-------------+------------------+
| 117 |    10 |           5 |           0.1400 |
| 118 |    10 |          10 |           0.0500 |
| 119 |    10 |          15 |           0.0100 |
| 120 |    10 |          20 |           0.0100 |
+-----+-------+-------------+------------------+
I would like to produce the following table:
+-------+--------------+-------------------+--------------+-------------------+--------------+-------------------+--------------+-------------------+
| hProp | iDateTarget1 | dblPercentTarget1 | iDateTarget2 | dblPercentTarget2 | iDateTarget3 | dblPercentTarget3 | iDateTarget4 | dblPercentTarget4 |
+-------+--------------+-------------------+--------------+-------------------+--------------+-------------------+--------------+-------------------+
|    10 |            5 |              0.14 |           10 |              0.05 |           15 |              0.01 |           20 |              0.01 |
+-------+--------------+-------------------+--------------+-------------------+--------------+-------------------+--------------+-------------------+

I have managed to do this using a pivot and then rejoining the original table several times, but I'm fairly sure there is a better way. This works as expected:


select
X0.hProp,
X0.iDateTarget1,
X1.dblTargetPercent [dblPercentTarget1],
X0.iDateTarget2,
X2.dblTargetPercent [dblPercentTarget2],
X0.iDateTarget3,
X3.dblTargetPercent [dblPercentTarget3],
X0.iDateTarget4,
X4.dblTargetPercent [dblPercentTarget4]
from (
    select
        hProp,
        max([1]) [iDateTarget1],
        max([2]) [iDateTarget2],
        max([3]) [iDateTarget3],
        max([4]) [iDateTarget4]
    from (
        select
            *,
            rank() over (partition by hProp order by iWeek) rank#
        from [Table X]
    ) T
    pivot (max(iWeek) for rank# in ([1],[2],[3], [4])) pv
    group by hProp
) X0
left join [Table X] X1 on X1.hprop = X0.hProp and X1.iWeek = X0.iDateTarget1
left join [Table X] X2 on X2.hprop = X0.hProp and X2.iWeek = X0.iDateTarget2
left join [Table X] X3 on X3.hprop = X0.hProp and X3.iWeek = X0.iDateTarget3
left join [Table X] X4 on X4.hprop = X0.hProp and X4.iWeek = X0.iDateTarget4


Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 21, 2025

To use SQL PIVOT for multiple columns, follow these steps depending on your database system. PIVOT is commonly used in SQL Server, while conditional aggregation is used in other databases like MySQL and PostgreSQL.


1. Using PIVOT in SQL Server

If you want to pivot multiple columns, use an aggregate function with PIVOT.

Example: Sales Data Pivoting (Year & Sales)

SELECT * 
FROM (
    SELECT SalesPerson, Year, Product, SalesAmount 
    FROM SalesData
) AS SourceTable  
PIVOT (
    SUM(SalesAmount) 
    FOR Year IN ([2022], [2023], [2024])
) AS PivotTable;

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 14, 2024

To pivot multiple columns in SQL, you can use the PIVOT function along with aggregation functions like MAX, MIN, SUM, etc., to transform row values into columns. Here's a general syntax for pivoting multiple columns:

SELECT pivot_column,
       [pivot_value1], [pivot_value2], ..., [pivot_valueN]
FROM (
    SELECT group_column, pivot_column, value_column1, value_column2, ..., value_columnN
    FROM your_table
) AS SourceTable
PIVOT (
    aggregation_function(value_column)
    FOR pivot_column IN ([pivot_value1], [pivot_value2], ..., [pivot_valueN])
) AS PivotTable;

Let's break down the syntax:

  • pivot_column: This is the column whose distinct values will become the new columns.
  • [pivot_value1], [pivot_value2], ..., [pivot_valueN]: These are the values from the pivot_column that you want to pivot into columns.
  • group_column: This column will remain in the result set to group the data.
  • value_column1, value_column2, ..., value_columnN: These are the columns whose values you want to pivot.
  • aggregation_function: It's applied to aggregate the values in value_column for each combination of group_column and pivot_column.

Here's a simple example to illustrate:

Let's say you have a table Sales with columns Year, Month, Product, and Revenue. You want to pivot the Product and Revenue columns based on the Year and Month.

  SELECT Year, Month,       [Product1_Revenue] AS Product1,       [Product2_Revenue] AS Product2,       [Product3_Revenue] AS Product3FROM (    SELECT Year, Month, Product, Revenue    FROM Sales) AS SourceTablePIVOT (    SUM(Revenue)    FOR Product IN ([Product1_Revenue], [Product2_Revenue], [Product3_Revenue])) AS PivotTable;

This query will pivot the Product and Revenue columns into separate columns for each Year and Month, summing up the revenues for each product.

Adjust the column names and aggregation functions (SUM, MAX, MIN, etc.) according to your specific requirements.

Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.