Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have an issue that I could not sort/order by Month or monthno in pivot.


Please advice me

Thank you
Maideen



here is my code

What I have tried:

SELECT * FROM (SELECT CurrCode,YEARNO,Monthno,TOTALAMOUNT
			FROM [dbo].[ML_tbl_Analaysis] where Currcode =@CURRCODE) tbl 
			PIVOT (sum(TOTALAMOUNT) FOR [yearno] IN ([2016],[2017],[2018],[2019],                                    [2020],[2021],[2022],[2023],[2024],[2025])) 
			as pivot_table 
Posted
Updated 27-Feb-20 20:05pm

1 solution

You need to use ORDER BY clause[^].
SQL
SELECT * 
FROM (
	SELECT CurrCode, YEARNO, Monthno, TOTALAMOUNT
	FROM [dbo].[ML_tbl_Analaysis]
	where Currcode =@CURRCODE
) tbl 
PIVOT (sum(TOTALAMOUNT) FOR [yearno] IN ([2016],[2017],[2018],[2019],[2020],[2021],[2022],[2023],[2024],[2025])) pvt
ORDER BY pvt.Monthno
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900