SQL Server - PIVOT - two columns into rows

后端 未结 2 704
你的背包
你的背包 2021-02-15 16:40

I saw many questions about PIVOT for a single column, each question more complex than other, however, I could not find anything like what I need.

To be honest, I don\'t

相关标签:
2条回答
  • 2021-02-15 17:09

    Try something like this

    CREATE TABLE #Table1
        ([uid] int, [name] varchar(4), [diseaseid] int, [intensity] varchar(4))
    ;
    
    INSERT INTO #Table1
        ([uid], [name], [diseaseid], [intensity])
    VALUES    (1, 'xxxx', 2, 'low')
        (1, 'xxxx', 1, 'high'),
    
    ;
    
    SELECT MAX([uid]) AS [uid]
           ,MAX([name]) AS [name]
           ,MAX([diseaseid1]) AS [diseaseid1]
           ,MAX([intensity1]) AS [intensity1]
           ,MAX([diseaseid2]) AS [diseaseid2]
           ,MAX([intensity2]) [intensity2]
    FROM 
    (
        SELECT [uid], [name]
        , CASE WHEN rn=2 THEN NULL ELSE [diseaseid] END AS [diseaseid1]
        , CASE WHEN rn=2 THEN NULL ELSE [intensity] END AS [intensity1]
        , CASE WHEN rn=1 THEN NULL ELSE [diseaseid] END AS [diseaseid2]
        , CASE WHEN rn=1 THEN NULL ELSE [intensity] END AS [intensity2]
        FROM
        (
            SELECT [uid], [name], [diseaseid], [intensity], 
            ROW_NUMBER() OVER(PARTITION BY [uid] ORDER BY Name) AS rn
            FROM #Table1
        ) T
    ) T
    GROUP BY [uid], [name]
    
    0 讨论(0)
  • 2021-02-15 17:11

    There are a few different ways that you can get the result that you want. Similar to @Sheela K R's answer you can use an aggregate function with a CASE expression but it can be written in a more concise way:

    select 
      max(case when rowid = 1 then first end) First1,
      max(case when rowid = 1 then last end) Last1,
      max(case when rowid = 2 then first end) First2,
      max(case when rowid = 2 then last end) Last2,
      max(case when rowid = 3 then first end) First3,
      max(case when rowid = 3 then last end) Last3,
      max(case when rowid = 4 then first end) First4,
      max(case when rowid = 4 then last end) Last4,
      max(case when rowid = 5 then first end) First5,
      max(case when rowid = 5 then last end) Last5
    from yourtable;
    

    See SQL Fiddle with Demo.

    This could also be written using the PIVOT function, however since you want to pivot multiple columns then you would first want to look at unpivoting your First and Last columns.

    The unpivot process will convert your multiple columns into multiple rows of data. You did not specify what version of SQL Server you are using but you can use a SELECT with UNION ALL with CROSS APPLY or even the UNPIVOT function to perform the first conversion:

    select col = col + cast(rowid as varchar(10)), value
    from yourtable
    cross apply 
    (
      select 'First', First union all
      select 'Last', Last
    ) c (col, value)
    

    See SQL Fiddle with Demo. This converts your data into the format:

    |    COL |       VALUE |
    |--------|-------------|
    | First1 | RandomName1 |
    |  Last1 | RandomLast1 |
    | First2 | RandomName2 |
    |  Last2 | RandomLast2 |
    

    Once the data is in multiple rows, then you can easily apply the PIVOT function:

    select First1, Last1, 
      First2, Last2,
      First3, Last3, 
      First4, Last4, 
      First5, Last5
    from
    (
      select col = col + cast(rowid as varchar(10)), value
      from yourtable
      cross apply 
      (
        select 'First', First union all
        select 'Last', Last
      ) c (col, value)
    ) d
    pivot
    (
      max(value)
      for col in (First1, Last1, First2, Last2,
                  First3, Last3, First4, Last4, First5, Last5)
    ) piv;
    

    See SQL Fiddle with Demo

    Both give a result of:

    |      FIRST1 |       LAST1 |      FIRST2 |       LAST2 |      FIRST3 |       LAST3 |      FIRST4 |       LAST4 |      FIRST5 |       LAST5 |
    |-------------|-------------|-------------|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
    | RandomName1 | RandomLast1 | RandomName2 | RandomLast2 | RandomName3 | RandomLast3 | RandomName4 | RandomLast4 | RandomName5 | RandomLast5 |
    
    0 讨论(0)
提交回复
热议问题