Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI Everyone,

I am using pivot function of sql. its working fine.
but i want to change the column sequence which pivot created

Following are example :

Table A :

Month | Amount | Gender | Name
jan 20 Male A
Feb 30 Female B
March 40 Male C

For this table i am using following query

SQL
 SELECT * FROM (
Select Month,Amount,Name From A
)AS  S
      PIVOT(
      SUM(Amount)
     FOR Name IN ([A],[B],[C]))AS p


this will give me output

Month |Gender| A |B |C
JAN Male 20 Null Null
Feb Female Null 30 Null
March Male Null Null 40


But I want The sequence of column like
Month,A,B,C then Gender

That mean column generated by pivot should come second and after that remaining column


NOTE : pivot generated Dynamically column that's why i am not aware about the column name
for example i will put a,b,c as column name in this example.


Thanx
Chetan V.
Posted

1 solution

You can refer this

how-to-change-the-column-order-of-an-existing-table-in-sql-server-2008[^]

Let me know if it is useful..
 
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