在透视选择声明中排序

时间:2022-03-04 10:08:24

This is my statement I need Order By date but I can't do this any help please

这是我的声明,我需要订购截止日期,但我不能做任何帮助请

My code

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

1 个解决方案

#1


You can alias ',' + QUOTENAME(sal_day_plan_0015.MyDate) as c and order by that column:

您可以将','+ QUOTENAME(sal_day_plan_0015.MyDate)别名为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: you have error in PIVOT keyword.

PS:你在PIVOT关键字中有错误。

#1


You can alias ',' + QUOTENAME(sal_day_plan_0015.MyDate) as c and order by that column:

您可以将','+ QUOTENAME(sal_day_plan_0015.MyDate)别名为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: you have error in PIVOT keyword.

PS:你在PIVOT关键字中有错误。