How to order month abbreviation and year in SQL, format : MMMYYYY

Solution for How to order month abbreviation and year in SQL, format : MMMYYYY
is Given Below:

I use the XML pivot to transpose the month columns as header and want to order it from the oldest month to current one, does anyone know how to do it?

enter image description here

Here is the code:

        @query AS NVARCHAR(MAX)

SELECT @cols  = STUFF((SELECT DISTINCT ','+ QUOTENAME(period_month) FROM  updated_order_raw_data
         FOR XML Path(''), TYPE).value('.','NVARCHAR(MAX)'),1,1,'')

-- the purpose of temp-table is for summarize the total
CREATE TABLE #temp_table (
        order_id varchar(max),
        total_amount numeric
INSERT INTO #temp_table

SELECT order_id, SUM(amount) as total_amount FROM updated_order_raw_data GROUP BY order_id

SET @query = 'SELECT * from 
                SELECT a.order_id, report_name, period_month, amount, b.total_amount as difference
                FROM updated_order_raw_data a
                LEFT JOIN #temp_table b
                on a.order_id = b.order_id
                ) Src
                FOR period_month IN  (' + @cols + ')
            ) Piv 
            WHERE difference <> 0
            ORDER BY order_id, report_name '

-- Drop temp-table
DROP TABLE #temp_table

Thanks Tashil Moorateeah,

I added order by here and it is workable.

SELECT @cols  = STUFF((SELECT DISTINCT ','+ QUOTENAME(period_month) AS [period_month] FROM  updated_order_raw_data ORDER BY [period_month] 
         FOR XML Path(''), TYPE).value('.','NVARCHAR(MAX)'),1,1,'')