1

我的原始数据如下所示:

在此处输入图像描述

我想执行 T-SQL 查询以将数据置于以下形式:

在此处输入图像描述

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

非常感谢您的帮助。

4

3 回答 3

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)

可以是任何聚合函数,因为每个函数只有一个值,这并不重要。

产生这个

在此处输入图像描述

编辑:如果您绝对需要最后修改的列,那么您需要像 bluefeet 所做的那样在子查询中添加连接。

于 2012-05-04T15:45:46.407 回答
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)
于 2012-05-04T15:46:12.983 回答
0

这实际上可以很容易地完成,而无需使用PIVOT. 这个想法是使用MAXcase 语句从分组中挑选出行并将它们变成列。您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

看到它在这里工作。

于 2012-05-04T17:20:40.583 回答