我写了这样的代码
SELECT * FROM TestTable
这会以垂直视图获取输出。
ID | Name
1 | John
2 | Mckensy
3 | Valneech
4 | Zeebra
但是,我需要水平显示它。
1 | 2 | 3 | 4
John | Mckensy | Valneech | Zeebra
怎么能这样做?
答案 0 :(得分:0)
您可以使用pivot来实现此要求。由于您还没有发布任何数据模型,我已经起草了一个粗略的逻辑。如果有效,请检查
静态
select * from your table
pivot (name for id in ([1],[2],[3],[4])) as anyrandomname
供您参考,您可以参考此链接。
https://blogs.msdn.microsoft.com/spike/2009/03/03/pivot-tables-in-sql-server-a-simple-sample/
答案 1 :(得分: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