使用Pivot的SQL查询帮助

时间:2011-08-29 02:24:11

标签: sql sql-server sql-server-2008 stored-procedures

我有一张表格如下:

PriorityText  Priority  LoRes   Partial  Unknown  N_A      HiRes
------------------------------------------------------------------
Very High     5         0.0612  0.0000   0.0612   0.0612   0.2041
High          4         0.1429  0.0000   0.1633   0.0000   0.1633
Medium        3         0.0000  0.0000   0.1020   0.0000   0.0408
Low-Medium    2         0.0000  0.0000   0.0000   0.0000   0.0000
Low           1         0.0000  0.0000   0.0000   0.0000   0.0000

我想将tbale转换成这个:

PriorityText  Low  Low-Medium  Medium  High    Very High
--------------------------------------------------------
Priority      1    2           3       4       5
LoRes         0    0           0       0.1429  0.0612
Partial       0    0           0       0       0
Unknown       0    0           0.102   0.1633  0.0612
N_A           0    0           0       0       0.0612
HiRes         0    0           0.0408  0.1633  0.2041

我正在使用SQL 2008.我正在努力解决SQL语法问题,以便对数据执行调整。

有人可以分享一个SQL代码片段来解决这个问题吗?

我已经使用以下方法成功转动了一行,但我不知道如何让它完成所有行。

SELECT VeryHigh AS VeryHigh, 
       High AS High, 
       Medium AS Medium, 
       [Low-Medium] AS [Low-Medium], 
       Low AS Low
 FROM  (SELECT [PriorityText], [LoRes], [Low-Medium], [Medium], [High], [VeryHigh] 
          FROM @tbTemp) p
PIVOT (SUM(LoRes) FOR [PriorityText] in ([VeryHigh], [High], [Medium], [Low-Medium], [Low])) pvt

我表中的测试数据如下:

Priority PriorityText   LoRes   Partial  Unknown   N_A    HiRes
1        VeryHigh       0.05    11       54        0      9
2        High           0.14    22       54        0      3
3        Medium         0.07    33       65        0      7
4        Low-Medium     0.01    44       87        0      4
5        Low            0       55       9         0      0
NULL     NULL           NULL    NULL     NULL      NULL   NULL

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

您需要UNPIVOT数据,然后使用所需的列标题重新PIVOT值:

SELECT  pvt.*
FROM
(    
    SELECT  unpvt.PriorityText
            ,unpvt.PriorityText2
            ,unpvt.MyValueMyValue
    FROM    SourceTable src
    UNPIVOT( MyValueMyValue FOR PriorityText2 IN ([Priority],[LoRes],[Partial],[Unknown],[N_A],[HiRes]) ) unpvt
) src2
PIVOT( MAX(src2.MyValueMyValue) FOR src2.PriorityText IN ([Low],[Low-Medium],[Medium],[High],[Very High]) ) pvt

答案 1 :(得分:0)

这个解决方案有点难看,但我认为它会做你想要的并且很直接。 有更多优雅和动态的方法来使用动态sql和xml转置数据。

例如http://sql-tricks.blogspot.com/2011/04/sql-server-rows-transpose.html

-- POPULATE SAMPLE DATA
DECLARE @tbTemp table (PriorityText varchar(50), Priority float, LoRes float, Partial float, Unknown float, N_A float, HiRes float)

insert into @tbTemp (PriorityText,Priority,LoRes,Partial,Unknown,N_A, HiRes)
values 
    ('Very High',5,0.0612,0.0000,0.0612,0.0612,0.2041),
    ('High',4,0.1429,0.0000,0.1633,0.0000,0.1633),
    ('Medium',3,0.0000,0.0000,0.1020,0.0000,0.0408),
    ('Low-Medium',2,0.0000,0.0000,0.0000,0.0000,0.0000),
    ('Low',1,0.0000,0.0000,0.0000,0.0000,0.0000)

;

with sourcetable ([Key],ColumnName,Value) -- Transposing into key/value-pair for each column
as
(
    select 'Priority', PriorityText, Priority from @tbTemp
    union all
    select 'LoRes', PriorityText, LoRes from @tbTemp
    union all
    select 'Partial', PriorityText, Partial from @tbTemp
    union all
    select 'Unknow', PriorityText, Unknown from @tbTemp
    union all
    select 'N_A', PriorityText, N_A from @tbTemp
    union all
    select 'HiRes', PriorityText, HiRes from @tbTemp
) 
select 
    grouptable.PriorityText,
        (select Value from sourcetable 
        where sourcetable.ColumnName = 'Low' 
        and sourcetable.[Key] = grouptable.PriorityText) as Low,
        (select Value from sourcetable 
        where sourcetable.ColumnName = 'Low-Medium' 
        and sourcetable.[Key] = grouptable.PriorityText) as [Low-Medium],
        (select Value from sourcetable 
        where sourcetable.ColumnName = 'Medium' 
        and sourcetable.[Key] = grouptable.PriorityText)as Medium,
        (select Value from sourcetable 
        where sourcetable.ColumnName = 'High' 
        and sourcetable.[Key] = grouptable.PriorityText) as High,
        (select Value from sourcetable 
        where sourcetable.ColumnName = 'Very High' 
        and sourcetable.[Key] = grouptable.PriorityText) as [Very High]
    from (
        select 'Priority' as PriorityText
        union all
        select 'LoRes' as PriorityText
        union all
        select 'Partial' as PriorityText
        union all
        select 'Unknow' as PriorityText     
        union all
        select 'N_A' as PriorityText        
        union all
        select 'HiRes' as PriorityText      
    ) grouptable -- Creating each row
相关问题