DECLARE @FirstDayOfWeek DATETIME
SELECT @FirstDayOfWeek =
COALESCE(
cast(@FirstDayOfWeek AS VARCHAR(MAX)) + ',[' + cast(@FirstDayOfWeek AS VARCHAR(MAX)) + ']',
'[' + cast(@FirstDayOfWeek AS VARCHAR(MAX))+ ']'
)
FROM [Calendar] c
LEFT JOIN Stores s ON c.FirstDayOfMonth = s.Validfrom
left join POSProductDivPeriod pos ON c.Period = pos.Period
DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N'
SELECT *
FROM (
SELECT
YEAR(FirstDayOfWeek) [Year],
@FirstDayOfWeek,
pos.quantity
FROM [Calendar] c
LEFT JOIN Stores s ON c.FirstDayOfMonth = s.Validfrom
left join POSProductDivPeriod pos ON c.Period = pos.Period
) AS PivotData
PIVOT (
SUM(pos.quantity)
FOR @FirstDayOfWeek IN (
' +CAST(@FirstDayOfWeek AS VARCHAR(MAX)) + '
)
) AS PivotTable
'
EXECUTE(@PivotTableSQL)
当我运行所有这些时,我得到命令完成 当我运行执行时,我必须声明疤痕 我试图为我的数据透视表运行我的代码,并在
之后立即执行任何想法?
答案 0 :(得分:0)