Question

I have a problem that has deals with multi column pivoting in SQL Server 2008. I would like to explain by an example.

Below is the result of joining 6 different tables using inner and left outer join

ID  Type    Date    Location    Result  Proc    ProcDate    ProcDetail        ProcNotes
--------------------------------------------------------------------------------------
1   ABC    1/1/2010   OK          AO    Proc_A  1/1/2013   This is Detail   Proc_A Notes
1   XYG    1/2/2011                     Proc_A  1/1/2013   This is Detail   Proc_A Notes
1   ABC    1/1/2010   OK          AO    Proc_B  1/1/2011   This is Detail   Proc_B Notes
1   XYG    1/2/2011                     Proc_B  1/1/2011   This is Detail   Proc_B Notes

Here are the details of above table

There are 2 types ABC and XYG and associated dates

Location and Result are associates with Type ABC

There are 2 Proc Proc_A and Proc_B and associated dates

ProcDetail and ProcNotes are associates with Proc Proc_A

None of the values are preknown

Instead of above resultset, I would like to have a result in a single row as below.

ID       Type1     Date1       Type2     Date2     Location   Result    Proc1     ProcDate1    ProcNotes1        ProcDetail           Proc2       ProcDate2      ProcNotes2
 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1        ABC     1/1/2010      XYZ     1/2/2011    OK         AO       Proc_A     1/1/2013    Proc_A Notes       This is Detail       Proc _B      1/1/2011         Proc_B Notes

Thanks for looking into it.

Was it helpful?

Solution

If values ABC and XYG are known upfront you can do conditional aggregation

SELECT ID,
       MAX(CASE WHEN type = 'ABC' THEN 'ABC' END) Type1,
       MAX(CASE WHEN type = 'ABC' THEN Date END) Date1,
       MAX(CASE WHEN type = 'XYG' THEN 'XYZ' END) Type2,
       MAX(CASE WHEN type = 'XYG' THEN Date END) Date2,
       MAX(Location) Location,
       MAX(Result) Result,
       MAX(CASE WHEN type = 'ABC' THEN [Proc] END) Proc1,
       MAX(CASE WHEN type = 'ABC' THEN ProcDate END) ProcDate1,
       MAX(CASE WHEN type = 'ABC' THEN ProcDetail END) ProcDetail1,
       MAX(CASE WHEN type = 'ABC' THEN ProcNotes END) ProcNotes1,
       MAX(CASE WHEN type = 'XYG' THEN [Proc] END) Proc2,
       MAX(CASE WHEN type = 'XYG' THEN ProcDate END) ProcDate2,
       MAX(CASE WHEN type = 'XYG' THEN ProcDetail END) ProcDetail2,
       MAX(CASE WHEN type = 'XYG' THEN ProcNotes END) ProcNotes2
  FROM
(
  SELECT * FROM table1 -- that's to emulate your current query with multiple joins
) q
 GROUP BY ID

Sample output:

| ID | TYPE1 |                          DATE1 | TYPE2 |                          DATE2 | LOCATION | RESULT |  PROC1 |                      PROCDATE1 |     PROCDETAIL1 |   PROCNOTES1 |  PROC2 |                      PROCDATE2 |     PROCDETAIL2 |   PROCNOTES2 |
|----|-------|--------------------------------|-------|--------------------------------|----------|--------|--------|--------------------------------|-----------------|--------------|--------|--------------------------------|-----------------|--------------|
|  1 |   ABC | January, 01 2010 00:00:00+0000 |   XYZ | January, 02 2011 00:00:00+0000 |       OK |     AO | Proc_B | January, 01 2013 00:00:00+0000 | This is Details | Proc_B Notes | Proc_B | January, 01 2013 00:00:00+0000 | This is Details | Proc_B Notes |

Here is SQLFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top