多列数据透视SQL Server

时间:2013-11-06 01:24:31

标签: sql database sql-server-2008 pivot unpivot

我有一个问题涉及SQL Server 2008中的多列透视。我想通过一个例子来解释。

以下是使用内外连接

连接6个不同表的结果
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

以下是上表

的详细信息

有两种类型的ABC和XYG以及相关的日期

位置和结果与ABC类型相关联

有2个Proc Proc_A和Proc_B及相关日期

ProcDetail和ProcNotes与Proc Proc_A

相关联

没有任何值是预先知道的

我希望在一行中得到一个结果,而不是上面的结果集。

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

感谢您对此进行调查。

1 个答案:

答案 0 :(得分:1)

如果预先知道值ABCXYG,则可以进行条件聚合

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

示例输出:

| 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 |

这是 SQLFiddle 演示