动态月份和年份枢轴,排序不当

Dynamically month and year pivot with bad sorting

根据答案 from here 我构建了一个适合我的解决方案,但我仍然有一个问题。

我有 table:

ID     |  Year  |  Month  |     Multiply    | Future |  Current
123    |  2017  |    1    |       1.0       |   25   |   null
123    |  2017  |    2    |       1.0       |   19   |   15
123    |  2017  |    3    |       1.0       |   13   |   0
123    |  2017  |    4    |       1.0       |   22   |   14
123    |  2017  |    5    |       1.0       |   13   |   null
...    |  ....  |   ...   |       ...       |   ..   |   ..
123    |  2018  |    1    |       1.0       |   25   |   10
123    |  2018  |    2    |       1.0       |   25   |   10
...    |  ....  |   ...   |       ...       |   ..   |   ..
124    |  2017  |    1    |       1         |   10   |   5
124    |  2017  |    2    |       1         |   15   |   2
...    |  ....  |   ...   |       ...       |   ..   |   ..
124    |  2018  |    1    |       1         |   20   |   0

我构建此视图以连接 Year + Month 并制作 IF 语句: 我从 Future 和 Current 列获得的新 Value 列中的值 - 当 Current 值为 null 时获取 Future 值并乘以 Multiply,否则获取 Current 值并乘以 Multiply(甚至 0)。在它旁边,当从 Future 列获取值时,我需要添加一个 'F' 前缀。

ID    |     Date      |    Value   |
123   |    2017 - 1   |     F25    |
123   |    2017 - 2   |     15     |
123   |    2017 - 3   |     0      |
..    |      ..       |     ..     |

代码:

SELECT  ID = ID,
        [Date] = [Date],
        [Value] = [Value]

FROM    (   SELECT  ID, 
                    cast([Year] as varchar(30)) + ' - ' + cast([Month]as varchar(30)) as [Date],
                    [Multiply],
                    case  when [Current] IS NULL /*OR [Current] = 0*/ 
                    then 'F' + CAST([Future] * [Multiply] as varchar(30))   
                    else CAST([Current] * [Multiply] as varchar(30))  
                    end as Value
            FROM    dbo.CurrentFuture
        ) AS t

然后我通过动态枢轴制作这个视图。

ID     |  2017 - 1  |  2017 - 10 |  2017 - 11 |  2017 - 12 |  2017 - 2 | ...  |  2018 - 1  |  ...
123    |    F25     |     ..     |     ..     |    ..      |     15    | ...  |    10      |  ...
124    |      5     |     2      |     ..     |    ..      |     ..    | ...  |     0      |  ...

代码:

    DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME([Date]) 
                    from dbo.UpperView
                    group by [Date]
                    order by [Date]
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT [ID],' + @cols + ' from 
             (
                select [ID], [Date],[Value]
                from [dbo].[UpperView]
            ) x
            pivot 
            (
                max([Value])
                for [Date] in (' + @cols + ')
            ) p '

execute(@query);

如您所见,新视图中的列排序不当。我用的是 2017 - 1、2017 - 2、2017 - 3,而不是 2017 - 1、2017 - 10、2017 - 11、 2017 - 12, 2017 - 2.你能帮我看看怎么正确排序吗?

根据有限的信息,你想要的是基于年+月的连接字符串的列排序。

您需要为一月至九月添加“0”前缀,而 October-December 不添加前缀。

所以实际上你会实现这个。

ID |  2017 - 01  |  2017 - 02 |  2017 - 03 |  ..... |  2017 - 09 |2018 - 10  |2018 - 11||2018 - 12|


   SELECT ID = ID,
    [Date] = [Date],
    [Value] = [Value]
   FROM
   (
      SELECT ID,
        CAST([Year] AS VARCHAR(30))+' - '+RIGHT('0'+CAST([Month] AS VARCHAR(30)), 2) AS [Date],
        [Multiply],
        CASE
               WHEN [Current] IS NULL 

        /*OR [Current] = 0*/

               THEN 'F'+CAST([Future] * [Multiply] AS VARCHAR(30))
               ELSE CAST([Current] * [Multiply] AS VARCHAR(30))
        END AS Value
      FROM dbo.CurrentFuture
   ) AS t;

向 UpperView 添加新列以便像这样排序

cast([Year] as varchar(30)) + RIGHT('0' + cast([Month] as varchar(30)), 2) as [DateOrder]

并在您的列查询中使用此列而不是 [Date]

进行排序
select @cols = STUFF((SELECT ',' + QUOTENAME([Date]) 
                    from dbo.UpperView
                    group by [Date], [DateOrder]
                    order by [DateOrder]
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')