How do you transpose rows into columns in a SQL query

Flemming Hald

I have this output:

Contact_Type    Category_Type        Category_Count
---------------------------------------------------
Window          Admissions              1775
Window          Financial Aid          17377
Window          Miscellaneous           2720
Window          Student Financials     14039
Phone           Admissions              5758
Phone           Financial Aid          10048
Phone           Miscellaneous           4497
Phone           Registration              11
Phone           Student Financials      4857

and this is my query:

SELECT 
    Contact_Type, Category_Type1, Category_Type2, Category_Type3,
    Category_Type4, Category_Type5 
FROM
    (SELECT 
         CASE
            WHEN event.contact_type = 0 THEN 'Window' 
            WHEN event.contact_type = 1 THEN 'Phone' 
         END AS Contact_Type,
         cat.category_type AS Category_Type,
         COUNT(ec.category_id) AS Category_Count,
         'Category_Type' + CAST(ROW_NUMBER() OVER (PARTITION BY Contact_Type
ORDER BY Contact_Type) AS varchar(20)) AS ColumnSequence
     FROM 
         yLines.ylines_event AS Event
     JOIN 
         ylines.ylines_event_category AS ec ON ec.event_id = event.event_id
     JOIN 
         ylines.ylines_category AS cat ON ec.category_id = cat.category_id
     WHERE /*event.contact_type = '0' AND*/    
         CAST(FORMAT(event.event_date_time, 'yyyy') AS int) BETWEEN 2014 AND dateadd(year, 1, event.event_date_time)
     GROUP BY 
         Category_Type, Contact_Type) a 
PIVOT
    (MAX(Contact_Type)
     FOR ColumnSequence IN (Category_Type1, Category_Type2, Category_Type3,
Category_Type4, Category_Type5)) as piv;   

If I run this it gives me an error:

Msg 207, Level 16, State 1, Line 1
Invalid column name 'Contact_Type'

and I can't seem to fix this. I am trying to transpose it so I see two rows only with 'Windows' and 'Phone' and the five Category Types transposed as five columns with the count in each. I am writing T-SQL statements. Please help!

n00bs

I would try do it in dynamic

       ; WITH [CONTACT]
     AS (
         SELECT *
           FROM (
                 VALUES 
                        ('Window', 'Admissions        ', ' 1775')
                      , ('Window', 'Financial Aid     ', '17377')
                      , ('Window', 'Miscellaneous     ', ' 2720')
                      , ('Window', 'Student Financials', '14039')
                      , ('Phone ', 'Admissions        ', ' 5758')
                      , ('Phone ', 'Financial Aid     ', '10048')
                      , ('Phone ', 'Miscellaneous     ', ' 4497')
                      , ('Phone ', 'Registration      ', '   11')
                      , ('Phone ', 'Student Financials', ' 4857')
                ) X ([Contact_Type], [Category_Type], [Category_Count])
     )   


     SELECT * 
       INTO #TEMP_PIVOT
       FROM [CONTACT]

    DECLARE @TYPE    VARCHAR(MAX)   
        SET @TYPE = STUFF( 
                          (SELECT DISTINCT ', ' + QUOTENAME(RTRIM(LTRIM([CATEGORY_TYPE])))
                                    FROM #TEMP_PIVOT
                                     FOR XML PATH('')
                          )
                          , 1, 1, '')

   DECLARE @SQL    VARCHAR(MAX)
       SET @SQL = '  SELECT [CONTACT_TYPE] '
                + '       , ' + @TYPE
                + '    FROM #TEMP_PIVOT '
                + '   PIVOT ( '
                + '           MAX([CATEGORY_COUNT]) '
                + '           FOR [CATEGORY_TYPE] IN (' + @TYPE + ')'
                + '         ) P '

     EXECUTE (@SQL)

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 do you transpose rows into columns in a SQL query

From Dev

transpose rows to columns in sql

From Dev

How do you transpose a dask dataframe (convert columns to rows) to approach tidy data principles

From Dev

SQL query to transpose rows to columns in MS Access table

From Dev

Transpose columns to rows SQL Server

From Dev

SQL: aggregate & transpose rows to columns

From Dev

SQL Server : Transpose rows to columns

From Dev

How do I return multiple columns and rows in a SQL Count Query?

From Dev

Transpose Columns or unpivot SQL Query

From Dev

How to transpose rows to columns in SQL or SSIS having dynamic values

From Dev

Oracle SQL Developer: How to transpose rows to columns using PIVOT function

From Dev

SQL Transpose Rows to undefined number of columns

From Dev

Transpose / Pivot Rows to Columns in SQL Server

From Dev

Conditional transpose columns into rows in oracle sql

From Dev

Transpose rows into columns SQL Server 2014

From Dev

SQL Transpose rows to columns without aggregate

From Dev

How to build a query that SELECT rows into columns for SQL?

From Dev

How to display columns as rows in a SQL query?

From Dev

How to merge two tables and transpose rows to columns

From Dev

How to transpose Columns to rows (with interruptions) in Excel?

From Dev

How to transpose grouped columns to rows with key

From Dev

How to transpose rows into columns in specific order in MATLAB?

From Dev

How to transpose dataframe columns into rows in pandas

From Dev

How to transpose multiple rows into multiple columns?

From Dev

How do I transpose a table in Pentaho Kettle from rows to columns without the header column

From Dev

Transpose rows and columns with no aggregate

From Dev

Transpose Rows to Columns With a Space

From Dev

Postgres - Transpose Rows to Columns

From Dev

Transpose the columns to rows

Related Related

  1. 1

    How do you transpose rows into columns in a SQL query

  2. 2

    transpose rows to columns in sql

  3. 3

    How do you transpose a dask dataframe (convert columns to rows) to approach tidy data principles

  4. 4

    SQL query to transpose rows to columns in MS Access table

  5. 5

    Transpose columns to rows SQL Server

  6. 6

    SQL: aggregate & transpose rows to columns

  7. 7

    SQL Server : Transpose rows to columns

  8. 8

    How do I return multiple columns and rows in a SQL Count Query?

  9. 9

    Transpose Columns or unpivot SQL Query

  10. 10

    How to transpose rows to columns in SQL or SSIS having dynamic values

  11. 11

    Oracle SQL Developer: How to transpose rows to columns using PIVOT function

  12. 12

    SQL Transpose Rows to undefined number of columns

  13. 13

    Transpose / Pivot Rows to Columns in SQL Server

  14. 14

    Conditional transpose columns into rows in oracle sql

  15. 15

    Transpose rows into columns SQL Server 2014

  16. 16

    SQL Transpose rows to columns without aggregate

  17. 17

    How to build a query that SELECT rows into columns for SQL?

  18. 18

    How to display columns as rows in a SQL query?

  19. 19

    How to merge two tables and transpose rows to columns

  20. 20

    How to transpose Columns to rows (with interruptions) in Excel?

  21. 21

    How to transpose grouped columns to rows with key

  22. 22

    How to transpose rows into columns in specific order in MATLAB?

  23. 23

    How to transpose dataframe columns into rows in pandas

  24. 24

    How to transpose multiple rows into multiple columns?

  25. 25

    How do I transpose a table in Pentaho Kettle from rows to columns without the header column

  26. 26

    Transpose rows and columns with no aggregate

  27. 27

    Transpose Rows to Columns With a Space

  28. 28

    Postgres - Transpose Rows to Columns

  29. 29

    Transpose the columns to rows

HotTag

Archive