Flatten a SQL Table from rows to columns allowing multiple results

R Kelly

I have an issue that is like a non-aggregating pivot, but I want my table to include multiple results. As an example I start out like this:

ID | OrganizationID | Group | Vendor | Product 
 1         3            G1      V1       P1
 2         3            G1      V2       P2
 3         3            G2      V3       P3
 4         4            G1      V1       P1
 5         4            G1      V1       P2

And need my data to end up like this:

ID | OrganizationID | G1_V1 | G1_V2 | G2_V3
 1         3            P1      P2      P3 
 2         4            P1     NULL    NULL
 3         4            P2     NULL    NULL

The data I am working with is around 7 million rows so self-joining with dynamic sql has proven extremely slow. Is there something easy I'm missing? Thanks in advance!

Joachim Isaksson

You can use a common table expression and a set of CASE statements to get the pivot structure you need; this will work identically in most RDBMS's.

WITH cte AS (
  SELECT id, organizationid, 
         CONCAT([Group],'_',Vendor) col, 
         product,
         ROW_NUMBER() OVER (
           PARTITION BY organizationid,[group],vendor 
           ORDER BY product) rn
  FROM myTable
)
SELECT 
  organizationid,
  MAX(CASE WHEN col='G1_V1' THEN product ELSE NULL END) G1_V1,
  MAX(CASE WHEN col='G1_V2' THEN product ELSE NULL END) G1_V2,
  MAX(CASE WHEN col='G2_V3' THEN product ELSE NULL END) G2_V3
FROM cte
GROUP BY organizationid, rn;

...or, alternately, you can use TSQL's regular PIVOT statement to replace the CASE part, which will probably be faster but may not work in all RDBMS's;

WITH cte AS (
  SELECT id, organizationid, 
         CONCAT([Group],'_',Vendor) col, 
         product,
         ROW_NUMBER() OVER (
           PARTITION BY organizationid,[group],vendor 
           ORDER BY product) rn
  FROM myTable
)
SELECT organizationid as organizationid, 
[G1_V1], [G1_V2], [G2_V3]
FROM
(SELECT organizationid,rn,col, product FROM cte) AS SourceTable
PIVOT
(
  MAX(product) FOR col IN ([G1_V1], [G1_V2], [G2_V3])
) AS PivotTable;

An SQLfiddle to test both.

You can produce a similar pivot in PostgreSQL using crosstab, however I don't have a PostgreSQL database with the tablefunc module installed to test on, so can only link to the details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How Can I Flatten the Results of a SQL Query - Transposing Rows to Columns?

From Dev

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

From Dev

SQL Results data from rows to columns

From Dev

SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

From Dev

SQL Combine multiple rows from one table into one row with multiple columns

From Dev

SQL join allowing multiple rows for given column

From Dev

Removing duplicate rows (based on values from multiple columns) from SQL table

From Dev

Update multiple columns with data retrieved from multiple rows of same table

From Dev

SQL Insert new columns in table B from rows of table A

From Dev

how to filter multiple rows in a sql table based on two columns

From Dev

Compare rows and multiple columns within same table SQL Server 2012

From Dev

Removal of Duplicate Rows from Data table Based on Multiple columns

From Dev

Jquery to hide table rows depending on multiple values from different columns

From Dev

Jquery to hide table rows depending on multiple values from different columns

From Dev

SELECT Columns FROM other Table but Should Return Multiple Rows

From Dev

Select rows from a table and use multiple interrelated columns for ordering

From Dev

From Multiple rows into multiple columns

From Dev

Add multiple columns and rows to a table

From Dev

Finding difference of two columns from two different rows in an SQL table

From Dev

SQL Server get rows from table and display in columns

From Dev

I need to flatten out SQL Server rows into columns using pivot

From Dev

SQL Server - Display columns from multiple rows as single row

From Dev

How to get columns from multiple rows in a single row in SQL

From Dev

SQL Server - Display columns from multiple rows as single row

From Dev

Retrieving data from specific columns of multiple rows in sql

From Dev

Pivot SQL table (Rows into Columns)

From Dev

SQL multiple rows as columns (optimizing)

From Dev

SQL: Convert multiple columns to rows

From Dev

Transform multiple columns in Excel table into multiple rows

Related Related

  1. 1

    How Can I Flatten the Results of a SQL Query - Transposing Rows to Columns?

  2. 2

    How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

  3. 3

    SQL Results data from rows to columns

  4. 4

    SQL: Select rows in a table by filtering multiple columns from the same table by a 3 column select result

  5. 5

    SQL Combine multiple rows from one table into one row with multiple columns

  6. 6

    SQL join allowing multiple rows for given column

  7. 7

    Removing duplicate rows (based on values from multiple columns) from SQL table

  8. 8

    Update multiple columns with data retrieved from multiple rows of same table

  9. 9

    SQL Insert new columns in table B from rows of table A

  10. 10

    how to filter multiple rows in a sql table based on two columns

  11. 11

    Compare rows and multiple columns within same table SQL Server 2012

  12. 12

    Removal of Duplicate Rows from Data table Based on Multiple columns

  13. 13

    Jquery to hide table rows depending on multiple values from different columns

  14. 14

    Jquery to hide table rows depending on multiple values from different columns

  15. 15

    SELECT Columns FROM other Table but Should Return Multiple Rows

  16. 16

    Select rows from a table and use multiple interrelated columns for ordering

  17. 17

    From Multiple rows into multiple columns

  18. 18

    Add multiple columns and rows to a table

  19. 19

    Finding difference of two columns from two different rows in an SQL table

  20. 20

    SQL Server get rows from table and display in columns

  21. 21

    I need to flatten out SQL Server rows into columns using pivot

  22. 22

    SQL Server - Display columns from multiple rows as single row

  23. 23

    How to get columns from multiple rows in a single row in SQL

  24. 24

    SQL Server - Display columns from multiple rows as single row

  25. 25

    Retrieving data from specific columns of multiple rows in sql

  26. 26

    Pivot SQL table (Rows into Columns)

  27. 27

    SQL multiple rows as columns (optimizing)

  28. 28

    SQL: Convert multiple columns to rows

  29. 29

    Transform multiple columns in Excel table into multiple rows

HotTag

Archive