return this one column into three columns

  • Hi friends...

    I have table with one column

    1

    2

    3

    4

    5

    6

    7

    8

    9

    And i should return this one column into three columns

    output like

    col1 col2 col3

    1 2 3

    4 5 6

    7 8 9

    in SSRS

  • Maybe something like this:

    WITH SampleData AS(

    SELECT 1 n UNION ALL

    SELECT 2 n UNION ALL

    SELECT 3 n UNION ALL

    SELECT 4 n UNION ALL

    SELECT 5 n UNION ALL

    SELECT 6 n UNION ALL

    SELECT 7 n UNION ALL

    SELECT 8 n UNION ALL

    SELECT 9 n

    )

    SELECT MAX(CASE WHEN n % 3 = 1 THEN n END) col1,

    MAX(CASE WHEN n % 3 = 2 THEN n END) col2,

    MAX(CASE WHEN n % 3 = 0 THEN n END) col3

    FROM SampleData

    GROUP BY (n - 1) / 3

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply