SQLTeam.com | Weblogs | Forums

Sum with Pivot Table

I would like your support to find a solution for my problem of duplicating several lines instead of being in a single line after executing my SQL code
I show my SQL code and a file of the result after executing the sql code and the result wishes
thank you

IF OBJECT_ID('dbo.TBL_TEMP') IS NOT NULL
DROP TABLE TBL_TEMP

DECLARE @SQLQuery AS NVARCHAR(MAX)
DECLARE @PivotColumns AS NVARCHAR(MAX)

SELECT @PivotColumns= COALESCE(@PivotColumns + ',','') + QUOTENAME(CONVERT(DATE,PROJECTFORECASTINVOICEDATE))
FROM [dbo].[ProjForecastSalesStaging]
WHERE [MODELID] = 'Cash flow'
GROUP BY CONVERT(DATE,PROJECTFORECASTINVOICEDATE)
ORDER BY CONVERT(DATE,PROJECTFORECASTINVOICEDATE) ASC

SET @SQLQuery =
N'SELECT [PROJECTID],[MODELID],[PROJECTLINEPROPERTYID],' + @PivotColumns + '
INTO TBL_TEMP
FROM [dbo].[ProjForecastSalesStaging]

PIVOT ( SUM(AMOUNT)
FOR PROJECTFORECASTINVOICEDATE IN (' + @PivotColumns + ') ) AS Q
WHERE Q.[MODELID] = ''Cash flow''
GROUP BY [PROJECTID],[MODELID],[PROJECTLINEPROPERTYID],' + @PivotColumns + ' '

EXEC sp_executesql @SQLQuery

Select TBL_TEMP.*
from TBL_TEMP WHERE PROJECTLINEPROPERTYID='011400'

Please provide ddl and dml for dbo].[ProjForecastSalesStaging
Help us help u. We dont have access to your database