2015-01-09 52 views
3

我無法從動態查詢結果中刪除空值。從動態查詢中刪除NULLS

這裏是什麼將成爲#T3表的例子:

BornDate | ClickDate | Clicks 
10/23/2014 | 11/19/2014 | 25 
10/23/2014 | 11/18/2014 | 6 
10/23/2014 | 11/20/2014 | 5 
10/23/2014 | 11/22/2014 | 17 
10/23/2014 | 11/23/2014 | 11 
10/24/2014 | 11/19/2014 | 1 
10/24/2014 | 11/18/2014 | 6 
10/24/2014 | 11/20/2014 | 3 
10/24/2014 | 11/21/2014 | 3 
10/24/2014 | 11/23/2014 | 2 

所以,我的問題是,我怎麼去除NULL值當我運行下面的查詢?

這裏是我的查詢

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 
DECLARE @ColumnName AS NVARCHAR(MAX) 

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
     + QUOTENAME(ClickDate) 
FROM (SELECT DISTINCT ClickDate FROM #t3) AS ClickDate order by ClickDate 

--Prepare the PIVOT query using the dynamic 

SET @DynamicPivotQuery = 
    'SELECT BornDate, ' + @ColumnName + ' 
    FROM #t3 
    PIVOT (SUM(Clicks) 
      FOR ClickDate IN (' + @ColumnName + ')) AS PVTTable order by 1, 2' 

--Execute the Dynamic Pivot Query 
EXEC sp_executesql @DynamicPivotQuery 

查詢的結果是:

| BORNDATE | 2014-11-18 | 2014-11-19 | 2014-11-20 | 2014-11-21 | 2014-11-22 | 2014-11-23| 
|------------|------------|------------|------------|------------|------------|-----------| 
| 2014-10-23 |   6 |   25 |   5 |  (null) |   17 |  11 | 
| 2014-10-24 |   6 |   1 |   3 |   3 |  (null) |   2 | 

你看到在2014年10月23日線在2014年11月21日列中的空值。再次爲2014年10月24日在列中的2014年10月24日行。我想替換這些空值。

+0

你在你的'clicks'列得到一個NULL並要顯示一個零呢?嘗試使用'SUM(COALESCE(Clicks,0))' – laylarenee 2015-01-09 21:23:00

+0

@laylarenee我相信-lee要求結果集沒有任何空值。 – 2015-11-18 13:39:18

回答

4

除了使用@ColumnName的值的兩個列表成爲新列,最終選擇列表中,您需要創建一個列名的單獨列表,將與零替換null - 類似isnull(yourcol, 0) as yourcol

我通常使用FOR XMLSTUFF來連接我的列名,所以你可以使用:

--Get null replacements of the PIVOT Column 
select @NullName = STUFF((SELECT ', IsNull(' + QUOTENAME(ClickDate)+', 0) as '+QUOTENAME(ClickDate) 
        from #t3 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

這將創建一個將用於最終的選擇列表中的列名的第二個第二。那麼你的PIVOT代碼將是:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 
DECLARE @ColumnName AS NVARCHAR(MAX) 
DECLARE @NullName AS NVARCHAR(MAX) 

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
     + QUOTENAME(ClickDate) 
FROM (SELECT DISTINCT ClickDate FROM #t3) AS ClickDate order by ClickDate 

--Get null replacements of the PIVOT Column 
select @NullName = STUFF((SELECT ', IsNull(' + QUOTENAME(ClickDate)+', 0) as '+QUOTENAME(ClickDate) 
        from #t3 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

--Prepare the PIVOT query using the dynamic 

SET @DynamicPivotQuery = 
    'SELECT BornDate, ' + @NullName + ' 
    FROM #t3 
    PIVOT (SUM(Clicks) 
      FOR ClickDate IN (' + @ColumnName + ')) AS PVTTable order by 1, 2' 

--Execute the Dynamic Pivot Query 
EXEC sp_executesql @DynamicPivotQuery 

請參閱SQL Fiddle with Demo。這給出了一個最終的結果:

| BORNDATE | 2014-11-18 | 2014-11-19 | 2014-11-20 | 2014-11-21 | 2014-11-22 | 2014-11-23| 
|------------|------------|------------|------------|------------|------------|-----------| 
| 2014-10-23 |   6 |   25 |   5 |   0 |   17 |  11 | 
| 2014-10-24 |   6 |   1 |   3 |   3 |   0 |   2 | 
3

要使用零替換空值:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX) 
DECLARE @ColumnName AS NVARCHAR(MAX) 
DECLARE @ColumnNameSelect AS NVARCHAR(MAX) 

--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
     + QUOTENAME(ClickDate) 
FROM (SELECT DISTINCT ClickDate FROM t3) AS ClickDate order by ClickDate 

--Get distinct values of the PIVOT Column with isnull for zero values 
SELECT @ColumnNameSelect= ISNULL(@ColumnNameSelect + ',','') 
     + 'isnull(' + QUOTENAME(ClickDate) + ',0) as ' + QUOTENAME(ClickDate) 
FROM (SELECT DISTINCT ClickDate FROM t3) AS ClickDate order by ClickDate 


--Prepare the PIVOT query using the dynamic 

SET @DynamicPivotQuery = 
    'SELECT BornDate, ' + @ColumnNameSelect + ' 
    FROM t3 
    PIVOT (SUM(Clicks) 
      FOR ClickDate IN (' + @ColumnName + ')) AS PVTTable order by 1, 2' 

--Execute the Dynamic Pivot Query 
EXEC sp_executesql @DynamicPivotQuery 

SQL FIDDLE