0

I have written a code like this

SELECT * FROM TestTable

This gets the output in a vertical view.

ID | Name
1  | John
2  | Mckensy
3  | Valneech
4  | Zeebra

However, I need to display it horizontally.

1    | 2       | 3        | 4
John | Mckensy | Valneech | Zeebra

How can one do this?

2 Answers2

0

You can use pivot for achieving the requirement. Since you have not posted any data model, I have drafted a rough logic. Please check it if works

Static

select * from your table
pivot (name for id in ([1],[2],[3],[4])) as anyrandomname

For your reference you can refer to this link.

https://blogs.msdn.microsoft.com/spike/2009/03/03/pivot-tables-in-sql-server-a-simple-sample/
Aritra Bhattacharya
  • 720
  • 1
  • 7
  • 18
0
select (select Name from test where ID=1) as 1,
       (select Name from test where ID=2) as 2,
       (select Name from test where ID=3) as 3,
       (select Name from test where ID=4) as 4
from test
group by 1,2,3,4
kostas
  • 461
  • 5
  • 13