2012-05-04 46 views
1

我有一个看起来原始数据如下:不知道如何在T-SQL这样做“合并”型查询

enter image description here

我想执行一个T-SQL查询来将数据放在下面的表格:

enter image description here

这几乎就像一个UNPIVOT类型的查询,但我不知道如何去这样做。还有一个澄清。 LastModifiedTime列的值可以是每个SubGroupId的五个值中的任何一个。它究竟是哪一个并不重要。

非常感谢您的帮助。

+0

不要看到你在您的文章包含的图片。 –

+0

您可以在SQL 2008中执行[PIVOT和UNPIVOT](http://msdn.microsoft.com/zh-cn/library/ms177410%28v=sql.100%29.aspx)。 –

+0

@David - 等待几秒钟 - 他们很慢,但确实出现。 –

回答

1

有两种方法可以做到这一点,无论是使用静态PIVOT,你硬编码,该值需要PIVOT:

create table temp 
(
    subgroupid int, 
    coater varchar(10), 
    metricTypeDescriptor varchar(5), 
    subgroupvalue decimal(20, 6), 
    valuedescription varchar(50), 
    lastmodfiedtime datetime 
) 

insert into temp values(28099, 'VA_A', 'CdSim', 2.99886, 'Average', '2012-05-03 20:55:02.14') 
insert into temp values(28099, 'VA_A', 'CdSim', 2.57201, 'Minimum', '2012-05-03 20:55:02.14') 
insert into temp values(28099, 'VA_A', 'CdSim', 3.1646, 'Maximum', '2012-05-03 20:55:02.143') 
insert into temp values(28099, 'VA_A', 'CdSim', 0.22082, 'Standard Deviation', '2012-05-03 20:55:02.153') 
insert into temp values(28099, 'VA_A', 'CdSim', 0.59259, 'xWebRange', '2012-05-03 20:55:02.157') 

insert into temp values(28102, 'VA_A', 'CdSim', 2.9091, 'Average', '2012-05-03 21:00:01.27') 
insert into temp values(28102, 'VA_A', 'CdSim', 2.67619, 'Minimum', '2012-05-03 21:00:01.27') 
insert into temp values(28102, 'VA_A', 'CdSim', 3.17471, 'Maximum', '2012-05-03 21:00:01.27') 
insert into temp values(28102, 'VA_A', 'CdSim', 0.191868, 'Standard Deviation', '2012-05-03 21:00:01.27') 
insert into temp values(28102, 'VA_A', 'CdSim', 0.49852, 'xWebRange', '2012-05-03 21:00:01.27') 

insert into temp values(28104, 'VA_A', 'CdSim', 3.05835, 'Average', '2012-05-03 21:05:01.383') 
insert into temp values(28104, 'VA_A', 'CdSim', 2.80336, 'Minimum', '2012-05-03 21:05:01.387') 
insert into temp values(28104, 'VA_A', 'CdSim', 3.17943, 'Maximum', '2012-05-03 21:05:01.387') 
insert into temp values(28104, 'VA_A', 'CdSim', 0.158743, 'Standard Deviation', '2012-05-03 21:05:01.39') 
insert into temp values(28104, 'VA_A', 'CdSim', 0.37607, 'xWebRange', '2012-05-03 21:05:01.40') 

select subgroupid, coater, metrictypedescriptor 
    , [average] 
    , [maximum] 
    , [minimum] 
    , [standard deviation] 
    , lastmodfiedtime 
from 
(
    select t1.subgroupid, t1.coater, t1.metrictypedescriptor, t1.valuedescription, t1.subgroupvalue, t2.lastmodfiedtime 
    from temp t1 
    left join 
    (
     select subgroupid, max(lastmodfiedtime) as lastmodfiedtime 
     from temp 
     group by subgroupid 
    ) t2 
     on t1.subgroupid = t2.subgroupid 
) x 
pivot 
(
    max(subgroupvalue) 
    for valuedescription in([Average], [Minimum], [Maximum], [Standard Deviation]) 
) p 


drop table temp 

或者,可以在您创建一个动态透视做到这一点在飞行中列的列表,然后转动它们:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.valuedescription) 
      FROM temp c 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

set @query = 'SELECT subgroupid, coater, metrictypedescriptor, ' + @cols + ', lastmodfiedtime from 
      (
       select t1.subgroupid, t1.coater, t1.metrictypedescriptor, t1.valuedescription, t1.subgroupvalue, t2.lastmodfiedtime 
       from temp t1 
       left join 
       (
        select subgroupid, max(lastmodfiedtime) as lastmodfiedtime 
        from temp 
        group by subgroupid 
       ) t2 
        on t1.subgroupid = t2.subgroupid 
       ) x 
      pivot 
      (
       max(subgroupvalue) 
       for valuedescription in(' + @cols + ') 
      ) p ' 

execute(@query) 
+0

感谢Bluefeet - 不错的工作。 – Hosea146

+0

@ Hosea146乐意帮忙。 :) – Taryn

1
select * from 
(
    select subgroupid, coater, metrictypedescription, subgroupvalue, valuedescription 
    from mytable 
) data 

PIVOT(
    sum(subgroupvalue) 
    FOR valuedescription IN ([Average],[Minimum],[Maximum],[Standard Deviation],[xWebRange]) 
    ) TOTALS 

-

sum(subgroupvalue) 

可以是任何一个聚合函数,因为你只能有一个值,每次其实并不重要。

息率这

enter image description here

编辑: 如果你绝对需要的最后一次修改列,那么你将需要添加的子查询的加入为bluefeet已经完成。

0

这其实可以不使用PIVOT很容易实现的。这个想法是使用MAX和一个case语句从你的分组中挑选出行并将它们变成列。您的GROUP BY是您希望返回的每个列在整个组中都是一致的。

SELECT SubGroupId, Coater, MetricTypeDescriptor, 
    MAX(CASE WHEN ValueDescription = 'Average' 
      THEN SubGroupValue END) as Average, 
    MAX(CASE WHEN ValueDescription = 'Minimum' 
      THEN SubGroupValue END) as Minimum, 
    MAX(CASE WHEN ValueDescription = 'Maximum' 
      THEN SubGroupValue END) as Maximum, 
    MAX(CASE WHEN ValueDescription = 'Standard Deviation' 
      THEN SubGroupValue END) as "Standard Deviation", 
    MAX(LastModifiedTime) as LastModifiedTime 
FROM YourTable 
GROUP BY SubGroupId, Coater, MetricTypeDescriptor 

See it working here.