SQL Server Pivot is inserting NULL values

user3333893

Please find the table (OututTable) that needs to be transposed. Here the QuestionID is formed by concatenating two values -[Question:AnswerID]

refID | SessionID | QuestionID     | AnswerValue
9000  | 205545715 | [4907]         | Good morning
12251 | 205543469 | [10576:16307]  | 3
12255 | 205543469 | [10907:17001]  | 4
13157 | 205543703 | [10576:16307]  | 3
14387 | 205543493 | [10907:17001]  | 2
14389 | 205543493 | [10911:17007]  | 3

The expected output should have one row per SessionID and the number of columns are dynamic

SessionID | [4097]         | [10576:16307] | [10907:17001] | [10911:17007]
205545715 |Good morning    |               |               |
205543469 |                |    3          |   4           |
205543703 |                |    3          |               |
205543493 |                |               |   2           |   3

I have the output in the above format but there are only NULL values inserted instead of Answer values

I am thinking there might a mismatch in column names. Any help would be great! please let me know.

Code:

    set @Questions = (STUFF((SELECT distinct ',[' + cast(i.SessionID as varchar(20))  + ']'          
            FROM OutputTable i
            FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, ''))

    print @Questions



    set @SQLQuery = 'select QuestionID,'+ @Questions +' from '+'('+ 'select SessionID,QuestionID,AnswerValue from OutputTable '+ ') p '+ 'PIVOT'+ '('+'max(Answervalue)'+'FOR p.SessionID IN ('+ @Questions +')' +') as pvt'
Ron Smith

Great Question! The problem is with the brackets in the QuestionID. While these are necessary for the Pivot Column Aliases, these don't work as string filters.

The code sample also switches QuestionID and SessionID for the expected output.

This code will return the expected output, sorted slightly differently. A temp table is created here to simulate the OutputTable object. This will need to be switched out with the DB Table.

declare
    @Questions varchar(max),
    @SQLQuery varchar(max)

create table #OutputTable
    (
    refID int,
    SessionID int,
    QuestionID varchar(50),
    AnswerValue varchar(50)
    )
insert into #OutputTable
    values
        (9000,205545715,'[4907]','Good morning'),
        (12251,205543469,'[10576:16307]','3'),
        (12255,205543469,'[10907:17001]','4'),
        (13157,205543703,'[10576:16307]','3'),
        (14387,205543493,'[10907:17001]','2'),
        (14389,205543493,'[10911:17007]','3')

set @Questions = (STUFF((SELECT distinct ',' + cast(i.QuestionID as varchar(20))          
    FROM #OutputTable i
    FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, ''))

set @SQLQuery = '
    select SessionID,'+ @Questions +'
    from    (
            select
                SessionID,
                replace(replace(QuestionID,''['',''''),'']'','''') QuestionID,
                AnswerValue
            from #OutputTable
            ) p 
        PIVOT (
              max(Answervalue)
              FOR p.QuestionID IN ('+ @Questions +')
              ) as pvt
    order by SessionID desc'

exec(@SQLQuery)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Returning "0" for NULL values within Dynamic Pivot for SQL Server

From Dev

Inserting hebrew values in SQL Server 2008

From Dev

SQL Server trigger not working while inserting values

From Dev

Inserting values in table using procedure SQL SERVER

From Dev

SQL server pivot query not taking null count

From Dev

Pivot and concatenate values from column in SQL Server

From Dev

SQL Server 2012 Pivot Dynamic with Values?

From Dev

Remove null values in SQL server

From Dev

Inserting Multiple values into a single null columns using sql

From Dev

How to convert multiple rows into one row with multiple columns using Pivot in SQL Server when data having NULL values

From Dev

Error inserting comma separated values in Table: SQL Server 2008

From Dev

Inserting arraylist values into the sql server database using java

From Dev

Inserting values to multiple columns in single row sql server

From Dev

NULL values returned by pivot

From Dev

SQL Pivot Query for pivot values

From Dev

SQL query should not return rows that contain null values in pivot table

From Dev

SQL Pivot - How To Show all rows with null values?

From Dev

SQL query should not return rows that contain null values in pivot table

From Dev

SQL Pivot - How To Show all rows with null values?

From Dev

Sql Server pivot command based on text giving null cells

From Dev

Sql Server pivot columns into rows fill NULL with zero or existing value

From Dev

Pivot values of field based on another field in TSQL / Microsoft SQL Server

From Dev

Not getting the correct SUM values in SQL Server 2012 when using a PIVOT

From Dev

PIVOT ignoring some values on MS SQL Server 2008?

From Dev

Treating Null values and duplicate records in SQL Server

From Dev

How to handle SQL Server NULL values

From Dev

Columns with Null Values in SQL Server vs Oracle

From Dev

No null values in any column in SQL Server

From Dev

SQL Server, combine tables with filling null values

Related Related

  1. 1

    Returning "0" for NULL values within Dynamic Pivot for SQL Server

  2. 2

    Inserting hebrew values in SQL Server 2008

  3. 3

    SQL Server trigger not working while inserting values

  4. 4

    Inserting values in table using procedure SQL SERVER

  5. 5

    SQL server pivot query not taking null count

  6. 6

    Pivot and concatenate values from column in SQL Server

  7. 7

    SQL Server 2012 Pivot Dynamic with Values?

  8. 8

    Remove null values in SQL server

  9. 9

    Inserting Multiple values into a single null columns using sql

  10. 10

    How to convert multiple rows into one row with multiple columns using Pivot in SQL Server when data having NULL values

  11. 11

    Error inserting comma separated values in Table: SQL Server 2008

  12. 12

    Inserting arraylist values into the sql server database using java

  13. 13

    Inserting values to multiple columns in single row sql server

  14. 14

    NULL values returned by pivot

  15. 15

    SQL Pivot Query for pivot values

  16. 16

    SQL query should not return rows that contain null values in pivot table

  17. 17

    SQL Pivot - How To Show all rows with null values?

  18. 18

    SQL query should not return rows that contain null values in pivot table

  19. 19

    SQL Pivot - How To Show all rows with null values?

  20. 20

    Sql Server pivot command based on text giving null cells

  21. 21

    Sql Server pivot columns into rows fill NULL with zero or existing value

  22. 22

    Pivot values of field based on another field in TSQL / Microsoft SQL Server

  23. 23

    Not getting the correct SUM values in SQL Server 2012 when using a PIVOT

  24. 24

    PIVOT ignoring some values on MS SQL Server 2008?

  25. 25

    Treating Null values and duplicate records in SQL Server

  26. 26

    How to handle SQL Server NULL values

  27. 27

    Columns with Null Values in SQL Server vs Oracle

  28. 28

    No null values in any column in SQL Server

  29. 29

    SQL Server, combine tables with filling null values

HotTag

Archive