Search code examples
sqlsql-serversql-order-bydynamic-pivot

Dynamically month and year pivot with bad sorting


From the answer from here I build a solution that is good for me but I have still one problem.

I had 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

I build this view to concatenate Year + Month and make IF statement: value in the new Value column I'm getting from Future and Current column - when the Current value is null get the Future value and multiply by Multiply, else get Current value and multiply by Multiply (even 0). Next to it I need to add a 'F' prefix when the value is got from Future column.

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

Code for it:

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

And from this I make this view via dynamically pivot.

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

Code for it:

    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);

As you can see columns in the new view are not sorting in a good way.. instead of 2017 - 1, 2017 - 2, 2017 - 3 I have 2017 - 1, 2017 - 10, 2017 - 11, 2017 - 12, 2017 - 2. Can you help me how to sort it properly?


Solution

  • From the limited information, What you want is the ordering of the column based on the Concatenated string of Year+ Month.

    What you need is to prefix the month with "0" for January - September and no prefix for October-December.

    so in effect you will achieve this.

    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;