You must Sign In to post a response.
  • Category: SQL Server

    Transposing or moving set of columns into rows


    This thread explains how to write T-sql to transpose the columns to rows in sql server



    In my database i have a table


    EmployeeID Feedbacks
    1234 feed1
    1234 feed2
    1234 feed3
    1234 feed4
    1234 feed5
    1234 feed6


    I want to transpose the same to the below published result set


    EmployeeID Feedback1 Feedback2 Feedback3 Feedback4 Feedback5 Feedback6
    1234 feed1 feed2 feed3 feed4 feed5 feed6


    Can anyone guide me to derive this using select query in sql server..?
  • #747046
    Hi,
    I think for your requirement we can use PIVOT concept.

    i have used third column just to meet the requirement

    check out following query which quite similar to your requirement


    CREATE TABLE test1( EID int,feedbacks nvarchar(10),qty int)

    INSERT INTO test1 VALUES(1234,'feed1',21), (1234,'feed2',23), (1234,'feed3',34), (1234,'feed4',45), (1234,'feed5',56)

    SELECT * FROM test1


    -- Pivot concept

    SELECT *
    FROM
    (
    SELECT EID ,feedbacks ,qty
    FROM test1
    ) AS source
    PIVOT
    (
    MAX(qty)
    FOR feedbacks IN ([feed1],[feed2],[feed3],[feed4],[feed5])
    ) as pvt

    Hope this will help

    Regards,
    Deep

  • #747059
    Hi,

    Use the below SQL Script for your requirement

    SELECT eid,
    [feed1] AS Feedback1,
    [feed2] AS Feedback2,
    [feed3] AS Feedback3,
    [feed4] AS Feedback4,
    [feed5] AS Feedback5
    FROM (SELECT eid,
    feedbacks
    FROM feed1) AS source
    PIVOT ( MAX(feedbacks)
    FOR feedbacks IN ([feed1],
    [feed2],
    [feed3],
    [feed4],
    [feed5]) ) AS pvt

    Regards
    Siva


  • Sign In to post your comments