SQL。 PIVOT返回NULL值

时间:2015-02-25 09:04:56

标签: sql sql-server dynamic null pivot

我简单地尝试将行转换为列,但PIVOT返回NULL值。在我尝试简单PIVOT之前,但未成功。目前我正在尝试Dynamic PIVOT,但结果仍然相同 - NULL值。

在枢轴之前(现在如何):

  1 test   -- Column alias name
----------
|  One   | -- Values which should be pivoted.
|  Two   | -- Values which should be pivoted.
|  Three | -- Values which should be pivoted.
|  Four  | -- Values which should be pivoted.
|  Five  | -- Values which should be pivoted.

转动之后(应该如何):

| Test1 | Test2 | Test3 | Test4 | Test5 | -- New columns alias names.
---------------------------------------------
|  One  |  Two  | Three | Four  | Five  | -- Pivoted values.

这是我的代码:

  DECLARE   @sql    AS NVARCHAR(MAX),
            @cols   AS NVARCHAR(MAX) = ''

  -- Below selecting new column names
    SELECT @cols += '[' + Name1 + '],' + 
                    '[' + Name2 + '],' + 
                    '[' + Name3 + '],' + 
                    '[' + Name4 + '],' + 
                    '[' + Name5 + '],' 
    FROM   (SELECT  'Test1' AS Name1, 
                    'Test2' AS Name2, 
                    'Test3' AS Name3, 
                    'Test4' AS Name4, 
                    'Test5' AS Name5                        
            ) a 

    SET @cols   = LEFT(@cols, LEN(@cols) - 1)
    SET @sql    = N'SELECT * 
    FROM 
    (
    -- If I selecting only this subquery (select clause) It returning values in 1 column (as shown in 'Before Pivot'), which should be pivoted to row (as shown in 'After Pivot')
        SELECT DISTINCT TOP 5   NA.ItemNo           AS  [1 test]                                                
        FROM        FormI                           AS  FI
        INNER JOIN  FormF                           AS  FF
        ON          FF.FormVId  = FI.FormVId
        INNER JOIN  FormV                           AS  FormV
        ON          FI.FormVId  = FormV.Id
        INNER JOIN  NavArt                          AS  NA
        ON          FI.ArticleId    = NA.Id
        WHERE       FI.WorkShiftId  = 10
    ) x 
    PIVOT
    (
        MIN ([1 test])
        FOR [1 test] IN (' + @cols + ') 
    ) p'
    EXEC Sp_executesql @sql

现在,此代码仅返回列名,但值为NULL。像那样:

| Test1 | Test2 | Test3 | Test4 | Test5 | -- New columns alias names.
---------------------------------------------
|  NULL |  NULL |  NULL |  NULL |  NULL | -- NULL instead of values

你有想法吗?

修改

我试图改变这个

SET @sql = N'SELECT * FROM (...

对此:

SET @sql = N'SELECT ''TestValue'' AS Test1, * 
            FROM 
            (...

在这种情况下,它会再返回一列Test1,但值为TestValue

|  Test1  | Test1 | Test2 | Test3 | Test4 | Test5 | -- 2x Test1 columns.
---------------------------------------------
|TestValue|  NULL |  NULL |  NULL |  NULL |  NULL | -- TestValue added

但我这样做只是为了测试目的,这对我来说不是好方法,因为价值观是动态的。

编辑2 现在我的@cols看起来像:

SELECT @cols += '[' + Name + '],' 
            FROM   (SELECT DISTINCT TOP 5   NA.ItemNo AS Name--, 
                            FROM        FormI                       AS  FI
                            INNER JOIN  FormF                       AS  FF
                            ON          FF.FormVId  = FI.FormVId
                            INNER JOIN  FormV                       AS  FormV
                            ON          FI.FormVId  = FormV.Id
                            INNER JOIN  NavArticles                 AS  NA
                            ON          FI.ArticleId        = NA.Id
                            WHERE       FI.WorkShiftId      = 10

                    ) a 

1 个答案:

答案 0 :(得分:1)

数据透视表中IN子句中使用的值必须与要旋转的列值匹配。如果要重命名列,可以在select语句中执行。这样的事情应该有效:

SELECT 
    [One Test]   as [NameOne], 
    [Two Test]   as [NameTwo], 
    [Three Test] as [NameThree], 
    [Four Test]  as [NameFour]
FROM (
SELECT DISTINCT TOP 4 NA.ItemId AS  [1 test]
FROM        .....               AS  FI
    INNER JOIN  .....           AS  FF
    ON          FF..= FI..
    INNER JOIN  ....            AS  FormV
    ON          FI..= FormV..
    INNER JOIN  ....            AS  NA
    ON          FI..= NA.Id
    WHERE       FI..= 10
) a
PIVOT
(
MIN([1 test])
FOR [1 test] IN ([One Test], [Two Test], [Three Test], [Four Test])
) AS piv;