仅在具有值时显示数据透视表列

时间:2017-04-05 12:35:38

标签: sql sql-server pivot

我正在和...斗争......

我有以下查询(Thanx到This

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


--Get distinct values of the PIVOT Column 
SELECT @ColumnName 
        = ISNULL(@ColumnName + ',','') 
        + QUOTENAME(Wcode)
FROM (SELECT DISTINCT WCode FROM DataInputTotal) AS WC

----Get distinct values of the PIVOT Column with isnull
SELECT @PivotSelectColumnNames 
    = ISNULL(@PivotSelectColumnNames + ',','')
    + 'ISNULL(' + QUOTENAME(WCode) + ', 0) AS '
    + QUOTENAME(WCode)
FROM (SELECT DISTINCT WCode FROM DataInputTotal) AS WC


--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT Top 10  FleetNo, LogNo, ProdDate, ShiftType, ' + @PivotSelectColumnNames + '
    FROM DataInputTotal
    PIVOT(SUM(Hrs) 
          FOR WCode IN (' + @ColumnName + ')) AS PVTTable
          WHERE PRODDATE BETWEEN ''20170328'' AND ''20170329'' AND SITENAME = ''MUMI'' 
                  ORDER By DataInputID DESC'
--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

所有运行都很顺利。我真正想要的是删除没有价值的列。有办法吗?

FleetNo LogNo           Date                    Shift   101     102     103     104     105
K601    N3292017601P    2017-03-29 00:00:00.000 Night   12.00   0.00    0.00    0.00    0.00
K601    D3292017601P    2017-03-29 00:00:00.000 Day     12.00   0.00    0.00    0.00    0.00
K601    N3282017601P.   2017-03-28 00:00:00.000 Night   12.00   0.00    0.00    0.00    0.00
K601    N3282017601P    2017-03-28 00:00:00.000 Day     12.00   0.00    0.00    0.00    0.00
K536    N3292017536P    2017-03-29 00:00:00.000 Night   12.00   0.00    0.00    0.00    0.00
TLB008  N3292017TLB008P 2017-03-29 00:00:00.000 Night   0.00    0.00    0.00    0.00    0.00
TLB004  N3292017TLB004P 2017-03-29 00:00:00.000 Night   0.00    0.00    0.00    0.00    0.00
TLB003  N3292017TLB003P 2017-03-29 00:00:00.000 Night   0.00    0.00    0.00    0.00    0.00
TLB002  N3292017TLB002P 2017-03-29 00:00:00.000 Night   0.00    0.00    0.00    0.00    0.00
K910    N3292017910P    2017-03-29 00:00:00.000 Night   0.00    0.00    0.00    0.00    0.00

我还应该指出,这将在userform上的DateGridView中表示。

改变剧本:

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

IF (SELECT object_id('TempDB..#TEMP')) IS NOT NULL
BEGIN
    DROP TABLE #Temp
END

SELECT * INTO #TEMP
FROM DataInputTotal 
WHERE PRODDATE BETWEEN '20170328' AND '20170329' AND SITENAME = 'MUMI'

--Get distinct values of the PIVOT Column 

SELECT @ColumnName 
        = ISNULL(@ColumnName + ',','') 
        + QUOTENAME(Wcode)
FROM (SELECT WCode 
     FROM #TEMP
     GROUP BY WCode HAVING SUM(Hrs) > 0) AS WC

----Get distinct values of the PIVOT Column with isnull
SELECT @PivotSelectColumnNames 
    = ISNULL(@PivotSelectColumnNames + ',','')
    + 'ISNULL(' + QUOTENAME(WCode) + ', 0) AS '
    + QUOTENAME(WCode)
FROM (SELECT WCode 
     FROM #TEMP 
     GROUP BY WCode HAVING SUM(Hrs) > 0) AS WC


--Prepare the PIVOT query using the dynamic 
SET @DynamicPivotQuery = 
  N'SELECT Top 10  FleetNo, LogNo, ProdDate, ShiftType, SiteName, ' + @PivotSelectColumnNames + '
    FROM #TEMP
    PIVOT(SUM(Hrs) 
          FOR WCode IN (' + @ColumnName + ')) AS PVTTable'

        --Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

1 个答案:

答案 0 :(得分:0)

您只需要限制@ColumnName生成。

--Get distinct values of the PIVOT Column 
SELECT @ColumnName 
        = ISNULL(@ColumnName + ',','') 
        + QUOTENAME(Wcode)
FROM 
(
     SELECT WCode 
     FROM DataInputTotal 
     WHERE PRODDATE BETWEEN '20170328' AND '20170329' AND SITENAME = 'MUMI'
     GROUP BY WCode HAVING SUM(Hrs) > 0
) AS WC

我建议创建一个存储过滤数据的表变量或临时表,以避免重复。像这样:

SELECT * INTO #TEMP
FROM DataInputTotal 
WHERE PRODDATE BETWEEN '20170328' AND '20170329' AND SITENAME = 'MUMI'

在开头并在以下语句中替换DataInputTotal。