Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I want to know is there any way to sort the SQL output data from "Left to Right (Column wise)" instead of "Top to Bottom (Row wise)" ?

Example : My data Input :-
---------------------------------
|Col1|Aug(Cnt)|Jul(Cnt)|Jun(Cnt)|Jul(%)|Jun(%)|Aug(%)|
|1|10|20|30|10|20|30|
|2|20|30|40|20|30|40|
|3|30|40|50|30|40|50|
------------------------------------
I Need Output Like :-
-------------------------------------
|Col1|Jun(Cnt)|Jun(%)|Jul(Cnt)|Jul(%)|Aug(Cnt)|Aug(%)|
|1|10|10|20|20|30|30|
|2|20|20|30|30|40|40|
|3|30|30|40|40|50|50|
--------------------------------------

Can anyone help me on this please.

Thanks in Advance.

What I have tried:

I really dont have any idea of doing this. Please help me.
Posted
Updated 16-Jan-17 6:30am
Comments
Afzaal Ahmad Zeeshan 16-Jan-17 9:57am    
SQL queries (especially, ORDER BY) are designed to work ROW-wise. They order the data based on the rows in a special column (such as ORDER BY Age, ORDER BY Name etc.). In other to have the data arranged based on the columns you would have to implement the structure yourself.

There is no way in SQL to automatically reorder columns according to the data they hold: if you think about it, what happens if two rows require a different order? Since SQL can start return information as soon as the first row of info is read (think SqlDataReader rather than SqlDataAdapter in presentation code) it would require the order to change dynamically.
 
Share this answer
 
Comments
Raj 0203 17-Jan-17 1:52am    
Thanks Griff,
Can you please elaborate your answer, or give me the required links to get that info.
Just change your query to select the columns in whatever order you want:
SQL
SELECT Col1, [Jun(Cnt)], [Jun(%)], [Jul(Cnt)], ...

But you shouldn't give your columns such horrible names. Stick to letters and numbers, with no spaces or special characters. If you want to change the caption of your column, do it in code, not in SQL.
 
Share this answer
 
Comments
Raj 0203 17-Jan-17 1:50am    
Hi Richard,
As i mentioned that was just an example data. And this kind of selection is not possible in my present situation.
Anyway Thanks for the support.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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