2016-08-02 14 views
1

これはちょうど私のストアドプロシージャは、組合から来ている部分は結果がグループでは、2008

Customer Posting Group  2015         2016 
JLC GMA    2262533.000000       2181239.500000 
IN HOUSE    0.000000         380.000000 
IN HOUSE    200.000000        2940.000000 
LUZ     241354.000000       265481.500000 
MIN     214083.500000       95776.500000 
VIS     315547.000000       378649.000000 
NUDERM GMA    1588089.000000       1991609.750000 
LUZ     574094.000000       717892.250000 
MIN     120125.750000       106789.000000 
VIS     202294.500000       258561.000000 

が、私は私の結果はGroup byカスタマーポスティングになりたいです

DECLARE @sql AS nvarchar(MAX) 

SET @sql = 'SELECT CASE 
WHEN [Customer Posting Group] IN (' + '''INHE''' + ',' + '''INH''' + ',' + '''IN HOUSE''' + ') THEN ' + '''IN HOUSE''' + ' 
WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''JLC''' + ' THEN ' + '''JLC GMA''' + ' 
WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''NUDERM''' + ' THEN ' + '''NUDERM GMA''' + ' 
ELSE [Customer Posting Group] 
END AS [Customer Posting Group] 
,ISNULL([' + CAST(@PrevYear AS NVARCHAR(10)) + '],0) as [' + CAST(@PrevYear AS NVARCHAR(10)) + '] 
,ISNULL([' + CAST(@YEARcurr AS NVARCHAR(10)) + '],0) as [' + CAST(@YEARcurr AS NVARCHAR(10)) + '] 
FROM #tmpPERregion       
PIVOT(SUM([GROSS AMOUNT]) 
FOR [MONTH YEAR] IN ([' + CAST(@PrevYear AS NVARCHAR(10)) + '],[' + CAST(@YEARcurr AS NVARCHAR(10)) + ']) 
) AS PivotSales' 

を選択していますグループ

答えて

0

以下の1つを試してみてください。構文的に間違っている可能性があります。同じことを確認する必要があります。

SET @sql = 'Select t.CustomerPostingGroup 
        ,ISNULL([' + CAST(@PrevYear AS NVARCHAR(10)) + '],0) as [' + CAST(@PrevYear AS NVARCHAR(10)) + '] 
        ,ISNULL([' + CAST(@YEARcurr AS NVARCHAR(10)) + '],0) as [' + CAST(@YEARcurr AS NVARCHAR(10)) + '] 
      From (
         Select CASE 
             WHEN [Customer Posting Group] IN (' + '''INHE''' + ',' + '''INH''' + ',' + '''IN HOUSE''' + ') THEN ' + '''IN HOUSE''' + ' 
             WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''JLC''' + ' THEN ' + '''JLC GMA''' + ' 
             WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''NUDERM''' + ' THEN ' + '''NUDERM GMA''' + ' 
             ELSE [Customer Posting Group] 
           END AS CustomerPostingGroup 
           ,Sum([GROSS AMOUNT]) As GROSSAMOUNT 
           ,[MONTH YEAR] As MONTHYEAR 
         From #tmpPERregion 
         Where [MONTH YEAR] In (' + @PrevYear + ',' + @YEARcurr + ') 
         Group By CASE 
             WHEN [Customer Posting Group] IN (' + '''INHE''' + ',' + '''INH''' + ',' + '''IN HOUSE''' + ') THEN ' + '''IN HOUSE''' + ' 
             WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''JLC''' + ' THEN ' + '''JLC GMA''' + ' 
             WHEN [Customer Posting Group] =' + '''GMA''' + ' AND [COMPANY]=' + '''NUDERM''' + ' THEN ' + '''NUDERM GMA''' + ' 
             ELSE [Customer Posting Group] 
           END 
           ,[MONTH YEAR] 
        ) As t 
      PIVOT(SUM([GROSSAMOUNT]) 
      FOR [MONTHYEAR] IN ([' + CAST(@PrevYear AS NVARCHAR(10)) + '],[' + CAST(@YEARcurr AS NVARCHAR(10)) + ']) 
      ) AS PivotSales' 
+0

私はすでにこのアプローチを行っています。それはt.CustomerPostingGroupの後に無効な列名を表示しています –

+0

データのサンプルがありますか? –

関連する問題