有关NULL值的PIVOT需要帮助

时间:2015-01-28 03:40:16

标签: sql-server pivot

我有这个数据集,想要做一个支点。

数据

Series_Name         Rec_Id  Series_Value    Point_Color
Hs avg CVD1 P1A     420     8810            NULL
Hs avg CVD1 P1B     420     8850.5          NULL
Hs avg CVD1 P2A     420     8793.5          R
Hs avg CVD1 P2B     420     8806.5          R

预期结果。

 Rec_Id Point_Color Hs avg CVD1 P1A     Hs avg CVD1 P1B     Hs avg CVD1 P2A     Hs avg CVD1 P2B
 420    R           8810                8850.5              8793.5              8806.5

但此时此刻,从我的尝试来看,这就是我得到的结果,我想这是NULL和' R'列Point_Color上的值导致它。请告知如何获得预期的结果?

我得到的当前结果

Rec_Id  Point_Color Hs avg CVD1 P1A     Hs avg CVD1 P1B     Hs avg CVD1 P2A     Hs avg CVD1 P2B
420     NULL        8810                8850.5              NULL                NULL
420     R           NULL                NULL                8793.5              8806.5

提前致谢。

2 个答案:

答案 0 :(得分:0)

使用此查询获得所需的结果。

SELECT Rec_Id,
      SUM([Hs avg CVD1 P1A]) AS [Hs avg CVD1 P1A], 
      SUM([Hs avg CVD1 P1B]) AS [Hs avg CVD1 P1B], 
      SUM([Hs avg CVD1 P2A]) AS [Hs avg CVD1 P2A], 
      SUM([Hs avg CVD1 P2b]) AS [Hs avg CVD1 P2b], 
      MAX(Point_Color) AS Point_Color
FROm #Test 
PIVOT(SUM(Series_Value) FOR Series_Name IN (
                                            [Hs avg CVD1 P1A], 
                                            [Hs avg CVD1 P1B], 
                                            [Hs avg CVD1 P2A], 
                                            [Hs avg CVD1 P2b]
                                            )
    ) AS P 
GROUP BY Rec_Id

答案 1 :(得分:0)

样本表

SELECT * INTO #TEMP
FROM
(
    SELECT 'Hs avg CVD1 P1A' Series_Name,      420 Rec_Id,     8810  Series_Value,           NULL Point_Color
    UNION ALL
    SELECT 'Hs avg CVD1 P1B',     420,     8850.5,          NULL
    UNION ALL
    SELECT 'Hs avg CVD1 P2A',     420,     8793.5,          'R'
    UNION ALL
    SELECT 'Hs avg CVD1 P2B',     420,     8806.5,          'R'
)TAB

由于您要在枢轴中对Point_Color进行分组,因此它会分别处理NULLR。为了避免NULLR分组,您需要将NULL替换为R以获得预期结果。

(1)静态转动。

SELECT Rec_Id, Point_Color,
MIN(CASE WHEN Series_Name='Hs avg CVD1 P1A' THEN  Series_Value END) [Hs avg CVD1 P1A],
MIN(CASE WHEN Series_Name='Hs avg CVD1 P1B' THEN  Series_Value END) [Hs avg CVD1 P1B],
MIN(CASE WHEN Series_Name='Hs avg CVD1 P2A' THEN  Series_Value END) [Hs avg CVD1 P2A],
MIN(CASE WHEN Series_Name='Hs avg CVD1 P2A' THEN  Series_Value END) [Hs avg CVD1 P2B]
FROM
(
    SELECT Rec_Id,ISNULL(Point_Color,'R') Point_Color,Series_Name,Series_Value  
    FROM #TEMP
)TAB
GROUP BY Rec_Id,Point_Color

(2)动态转动

如果Series_Name中的值未提前知道,您可以使用动态数据透视。

获取透视列。

DECLARE @cols NVARCHAR (MAX)

SELECT @cols = COALESCE (@cols + ',[' + Series_Name + ']', '[' + Series_Name + ']')
               FROM    (SELECT DISTINCT Series_Name FROM #TEMP) PV 
               ORDER BY Series_Name

现在转动它。

DECLARE @query NVARCHAR(MAX)
SET @query = 'SELECT * FROM 
             (
                 SELECT Series_Name,Rec_Id,Series_Value ,ISNULL(Point_Color,''R'') Point_Color  
                 FROM #TEMP
             ) x
             PIVOT 
             (
                 MIN(Series_Value)
                 FOR Series_Name IN (' + @cols + ')
            ) p
            ORDER BY Rec_Id;' 

EXEC SP_EXECUTESQL @query

<强> RESULT

enter image description here