2012-09-21 31 views
2

我想在Sql Server中顯示行到列。我已經看到了其他問題,但這些列在主數據中硬編碼,但我的列將是動態的。到目前爲止我所取得的成就。如截圖所示,我能夠行轉換成列,但幾件事情我不能做到。需要你的幫助guyz行SQL SERVER中的列使用PIVOT命令(將NULL值替換爲0並顯示SUM的所有值)

  1. 更換爲null,0中的所有列
  2. 需要加1多列,將顯示所有列的總和除companyID

我的SQL代碼:

DECLARE @Columns VARCHAR(MAX) 
DECLARE @Convert VARCHAR(MAX) 

SELECT @Columns = STUFF((
      SELECT '],[' + ErrClassfn 
      from ArchimedesTables.dbo.PM_ErrClassificationSetup 
      WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
          Between CONVERT(VARCHAR(10), GETDATE(), 101) 
          AND  CONVERT(VARCHAR(10), GETDATE(), 101) 
      ORDER BY '],[' + CONVERT(VARCHAR(MAX), ID) ASC 
       FOR 
        XML PATH('') 
      ), 1, 2, '') + ']' 

SET @Convert = 'SELECT * INTO #mynewTable FROM 
(
    SELECT COUNT(WQ.ErrClassfnID) as ErrorCount, UPPER(WQ.CompanyID) as CompanyID, 
      PME.ErrClassfn as ErrorName 
     FROM Version25.dbo.WF_Quality AS WQ 
     LEFT JOIN ArchimedesTables.dbo.PM_ErrClassificationSetup as PME 
      ON WQ.ErrClassfnID = PME.ID 
    GROUP BY 
      UPPER(CompanyID), ErrClassfn 
) Quality PIVOT (SUM(ErrorCount) For ErrorName IN (' + @Columns 
    + ')) as PivotTable SeLeCt * FROM #mynewTable' 

EXEC(@Convert) 

enter image description here

+0

在Bluefeet的回答後,我刪除了我正在處理的那個。爲了補充他的答案 - 你可以將總和添加到@convert,把這個SUM(COUNT(WQ.ErrClassfnID))OVER(PARTITION BY UPPER(CompanyID))AS Total'放在選擇列表的末尾並添加「total」 。 –

回答

2

您可以更改列名,等,爲動態透視,與此類似:

DECLARE @ColumnsNull VARCHAR(MAX) 
DECLARE @Columns VARCHAR(MAX) 
DECLARE @Convert VARCHAR(MAX) 

SELECT @ColumnsNull = STUFF((SELECT ', IsNull(' + QUOTENAME(ErrClassfn) +', 0) as ['+ rtrim(ErrClassfn)+']' 
        from ArchimedesTables.dbo.PM_ErrClassificationSetup 
        WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
            Between CONVERT(VARCHAR(10), GETDATE(), 101) 
            AND  CONVERT(VARCHAR(10), GETDATE(), 101) 
        ORDER BY ID ASC 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 

SELECT @Columns = STUFF((
      SELECT '],[' + ErrClassfn 
      from ArchimedesTables.dbo.PM_ErrClassificationSetup 
      WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
          Between CONVERT(VARCHAR(10), GETDATE(), 101) 
          AND  CONVERT(VARCHAR(10), GETDATE(), 101) 
      ORDER BY '],[' + CONVERT(VARCHAR(MAX), ID) ASC 
       FOR 
        XML PATH('') 
      ), 1, 2, '') + ']' 


SET @Convert = 'SELECT CompanyID, '+ @ColumnsNull + ' 
       INTO #mynewTable 
       FROM 
       (
        SELECT COUNT(WQ.ErrClassfnID) as ErrorCount, UPPER(WQ.CompanyID) as CompanyID, 
          PME.ErrClassfn as ErrorName 
         FROM Version25.dbo.WF_Quality AS WQ 
         LEFT JOIN ArchimedesTables.dbo.PM_ErrClassificationSetup as PME 
          ON WQ.ErrClassfnID = PME.ID 
        GROUP BY 
          UPPER(CompanyID), ErrClassfn 
       ) Quality PIVOT (SUM(ErrorCount) For ErrorName IN (' + @Columns 
        + ')) as PivotTable SeLeCt * FROM #mynewTable' 

EXEC(@Convert) 

我會建議編寫查詢並獲得列的第一工作,然後將數據添加到#temp表。這樣調試會更容易。

您還可以創建一個SUM()場以同樣的方式,在那裏你動態地構建它,然後在最後SELECT添加:

所以它可能是這樣的,你可以加入到最終的SELECT

SELECT @ColumnsTotal = STUFF((SELECT '+' + QUOTENAME(ErrClassfn) 
        from ArchimedesTables.dbo.PM_ErrClassificationSetup 
        WHERE CONVERT(VARCHAR(10), ISNULL(EndDate, GETDATE()), 101) 
            Between CONVERT(VARCHAR(10), GETDATE(), 101) 
            AND  CONVERT(VARCHAR(10), GETDATE(), 101) 
        ORDER BY ID ASC 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 
+0

謝謝BlueFeet ... :)你的Sql知識Rocks :) –

相關問題