Pivoting non-numeric data that spans across multiple rows in SQL can be challenging, but it can be done using the following steps:
Identify the columns to include in the pivot: Choose the column(s) that will be used as row headers and the column(s) that will be used as column headers. These columns should be non-numeric.
Use a recursive CTE to create a row number for each distinct value in the row header column. This will allow us to group the values by row when pivoting.
Use a self-join to combine the rows that belong to the same group. This will create a single row for each group that includes all the values that should be pivoted.
Use the PIVOT function to pivot the data based on the column header values.
Here is an example query that pivots non-numeric data that spans across multiple rows:
WITH RecursiveCTE AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY RowHeader ORDER BY SortOrder) AS RowNum,
RowHeader,
ColumnHeader,
ColumnValue
FROM PivotingTable
), CombinedRows AS (
SELECT
RC1.RowHeader,
RC1.RowNum,
RC1.ColumnHeader,
RC1.ColumnValue,
RC2.ColumnHeader AS ColumnHeader2,
RC2.ColumnValue AS ColumnValue2
FROM RecursiveCTE RC1
LEFT JOIN RecursiveCTE RC2
ON RC1.RowHeader = RC2.RowHeader AND RC1.RowNum = RC2.RowNum - 1
), PivotTable AS (
SELECT
*
FROM CombinedRows
PIVOT (
MAX(ColumnValue2)
FOR ColumnHeader2 IN ([Column1], [Column2], [Column3])
) AS PivotTable
)
SELECT
RowHeader,
[Column1],
[Column2],
[Column3]
FROM PivotTable
In this example, we assume that we have a table named PivotingTable with the following columns: RowHeader, ColumnHeader, ColumnValue, and SortOrder. The RowHeader column contains the values that will be used as row headers in the pivot table. The ColumnHeader column contains the values that will be used as column headers in the pivot table. The ColumnValue column contains the values that will be pivoted. The SortOrder column is used to order the rows within each group.
The query starts by using a recursive CTE to assign a row number to each distinct value in the RowHeader column. It then uses a self-join to combine the rows that belong to the same group. Finally, it uses the PIVOT function to pivot the data based on the ColumnHeader values.
Note that the column headers must be hard-coded in the PIVOT statement. This means that you must know the possible values of the ColumnHeader column in advance. If the possible values are unknown or dynamic, you may need to use dynamic SQL to generate the pivot statement dynamically based on the actual values.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-12-07 11:00:00 +0000
Seen: 7 times
Last updated: Dec 26 '21
What is the method for programmatic access to a time series?
What is the procedure for using pg_restore on Windows with Docker?
Can SqlDependency be used in a programming language other than .NET, such as node js?
How can multiple queries be merged into a single stored procedure in MySQL?
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?