不知道如何在T-SQL中执行此“合并”类型查询

时间:2012-05-04 15:28:57

标签: sql-server-2008 tsql

我的原始数据如下所示:

enter image description here

我想执行T-SQL查询以将数据放入以下格式:

enter image description here

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

非常感谢你的帮助。

3 个答案:

答案 0 :(得分: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所做的那样。

答案 1 :(得分: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

或者你可以通过一个动态的Pivot来实现这一点,你可以动态创建列列表,然后将它们PIVOT:

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)

答案 2 :(得分: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.