2013-10-15 20 views
1

我有一个看起来如下数据:是否可以像这样旋转数据?

enter image description here

我想,这样只有在SUBID单行转动这个数据。该栏目将SUBID,802Lineage,802ReadTime,1000Lineage,1000ReadTime等

如果它是不要求有包括天堂,这将是非常简单的,如下:

Select SubId, [800] as [800Time], [1190] as [1190Time], [1605] as [1605Time] 
From 
(Select SubId, ProcessNumber, ReadTime From MyTable) as SourceTable 
PIVOT 
(
    Max(ReadTime) 
    For ProcessNumber IN ([800],[802],[1190],[1605]) 
) as PivotTable; 

我我不知道如何在包括Lineage的情况下做到这一点。这是SQL Server 2012

+0

为什么不加入SubId上的两个枢纽? –

+0

你能举一个例子吗? –

回答

1

可以手动转动:

select 
    SubId, 
    max(case when ProcessNumber = 802 then ReadTime end) as [802Time], 
    max(case when ProcessNumber = 802 then Lineage end) as [802Lineage], 
    .... 
from SourceTable 
group by SubId 
+0

嗯......有趣的想法。谢谢。 –

+0

@RandyMinder这种支点非常灵活,如果你喜欢,你可以混合不同的聚合体。它确实需要更多的代码,但对我而言,未来的扩展比标准的数据透视容易得多 –

1

连接两个数据透视表,在意见中的要求的例子。

CREATE TABLE #MyTable (SubId int, ProcessNumber int, Lineage varchar(16), ReadTime datetime) 
INSERT INTO #MyTable  
     (SubID, ProcessNumber, Lineage, ReadTime) 
VALUES 
     (1, 9, 'A', GETDATE()), 
     (1, 8, 'A', GETDATE()), 
     (1, 7, 'B', GETDATE()), 
     (2, 9, 'C', GETDATE()), 
     (2, 8, 'C', GETDATE()) 
SELECT *  
FROM (
    Select SubId, [9] as [9Time], [8] as [8Time], [7] as [7Time] 
    From 
    (Select SubId, ProcessNumber, ReadTime From #MyTable) as SourceTable 
    PIVOT(Max(ReadTime) For ProcessNumber IN ([9],[8],[7],[6])) as PivotTable1 
) AS T1 
INNER JOIN (
    Select SubId, [9] as [9Lineage], [8] as [8Lineage], [7] as [7Lineage] 
    From 
    (Select SubId, ProcessNumber, Lineage From #MyTable) as SourceTable 
    PIVOT(Max(Lineage) For ProcessNumber IN ([9],[8],[7],[6])) as PivotTable1 
) AS T2 
ON T1.SubId = T2.SubId 
GO 
1

可以使用旋转功能来获得结果,但你必须从多列LineageReadTime列逆转置成多行。

由于您使用的是SQL Server 2012中您可以使用UNPIVOT CROSS应用与值的数据:

select subid, 
    colname = cast(processNumber as varchar(10)) + colname, 
    value 
from mytable 
cross apply 
(
    values 
    ('Lineage', Lineage), 
    ('ReadTime', convert(varchar(20), readtime, 120)) 
) c (colname, value) 

SQL Fiddle with Demo。这将当前数据转换成格式:

|  SUBID |  COLNAME |    VALUE | 
|-------------|--------------|---------------------| 
| 12010231146 | 802Lineage |    PBG12A | 
| 12010231146 | 802ReadTime | 2012-01-02 21:44:00 | 
| 12010231146 | 1000Lineage |    PBG12A | 
| 12010231146 | 1000ReadTime | 2012-01-02 21:43:00 | 
| 12010231146 | 1190Lineage |    PBG11B | 
| 12010231146 | 1190ReadTime | 2012-01-03 14:36:00 | 

一旦数据以这种格式,那么你可以很容易地应用旋转功能,让您的最终结果:

select * 
from 
(
    select subid, 
    colname = cast(processNumber as varchar(10)) + colname, 
    value 
    from mytable 
    cross apply 
    (
    values 
     ('Lineage', Lineage), 
     ('ReadTime', convert(varchar(20), readtime, 120)) 
) c (colname, value) 
) d 
pivot 
(
    max(value) 
    for colname in ([802Lineage], [802ReadTime], 
        [1000Lineage], [1000ReadTime], 
        [1190Lineage], [1190ReadTime]) 
) piv; 

SQL Fiddle with Demo

上述工程巨大的,如果你有,你要转换行的数量有限,但如果你有一个未知号码,然后你可以使用动态SQL:

DECLARE @cols AS NVARCHAR(MAX), 
    @query AS NVARCHAR(MAX) 

select @cols = STUFF((SELECT ',' + QUOTENAME(cast(processnumber as varchar(10))+col) 
        from mytable 
        cross apply 
        (
         select 'Lineage', 0 union all 
         select 'ReadTime', 1 
        ) c (col, so) 
        group by processnumber, col, so 
        order by processnumber, so 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 


set @query = 'SELECT subid, ' + @cols + ' 
      from 
      (
       select subid, 
       colname = cast(processNumber as varchar(10)) + colname, 
       value 
       from mytable 
       cross apply 
       (
       values 
        (''Lineage'', Lineage), 
        (''ReadTime'', convert(varchar(20), readtime, 120)) 
      ) c (colname, value) 
      ) x 
      pivot 
      (
       max(value) 
       for colname in (' + @cols + ') 
      ) p ' 

execute sp_executesql @query; 

SQL Fiddle with Demo。这给出了一个结果:

|  SUBID | 802LINEAGE |   802READTIME | 1000LINEAGE |  1000READTIME | 1190LINEAGE |  1190READTIME | 1605LINEAGE |  1605READTIME | 1745LINEAGE |  1745READTIME | 1790LINEAGE |  1790READTIME | 1990LINEAGE |  1990READTIME | 2690LINEAGE |  2690READTIME | 2795LINEAGE |  2795READTIME | 2990LINEAGE |  2990READTIME | 3090LINEAGE |  3090READTIME | 3290LINEAGE |  3290READTIME | 
|-------------|------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------|-------------|---------------------| 
| 12010231146 |  PBG12A | 2012-01-02 21:44:00 |  PBG12A | 2012-01-02 21:43:00 |  PBG11B | 2012-01-03 14:36:00 |  PBG11B | 2012-01-03 15:15:00 |  PBG11A | 2012-01-03 15:16:00 |  PBG11A | 2012-01-03 15:19:00 |  PBG11A | 2012-01-03 15:23:00 |  PBG11A | 2012-01-03 15:32:00 |  PBG11A | 2012-01-03 15:39:00 |  PBG11A | 2012-01-03 15:41:00 |  PBG11A | 2012-01-03 15:46:00 |  PBG11A | 2012-01-03 15:47:00 | 
相关问题