这是我需要Order By date
的陈述,但我无法提供任何帮助
我的代码
PROCEDURE [sal_pivottable_day_plan]
AS
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@DtFrom date, @DtTo date
SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(sal_day_plan_0015.MyDate) FROM sal_day_plan_0015
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
SET @query = 'SELECT dealers_sril, dealers_name, stop_value, hanging_value, dealers_balanc, paid_val_motkhrat, ' + @cols + '
FROM (
SELECT dealers_sril, dealers_name, stop_value, hanging_value, dealers_balanc, paid_val_motkhrat, paid_val_between,MyDate
FROM sal_day_plan_0015
)x
PIOVT
(
min(paid_val_between)for MyDate in (' + @cols + ')
) p'
EXEC(@query)
RETURN 0
答案 0 :(得分:1)
您可以别名',' + QUOTENAME(sal_day_plan_0015.MyDate) as c
并按该列排序:
PROCEDURE [sal_pivottable_day_plan]
AS
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX),@DtFrom date ,@DtTo date
SELECT @cols = STUFF((SELECT distinct ',' + QUOTENAME(sal_day_plan_0015.MyDate) AS c FROM sal_day_plan_0015
ORDER BY c
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
SET @query = 'SELECT dealers_sril, dealers_name, stop_value, hanging_value, dealers_balanc, paid_val_motkhrat, ' + @cols + '
FROM (
SELECT dealers_sril, dealers_name, stop_value, hanging_value, dealers_balanc, paid_val_motkhrat, paid_val_between,MyDate
FROM sal_day_plan_0015
)x
PIVOT
(
min(paid_val_between)for MyDate in (' + @cols + ')
) p'
EXEC(@query)
RETURN 0
PS:PIVOT
关键字中存在错误。