sql 行转列

select tbl2.id,tbl2.TitleName,tbl1.[1],tbl1.[2],tbl1.[3],tbl1.[4] from
(select * from
(SELECT [MainId] ,[SAnswer] ,Row_Number() OVER(partition by mainid order by MainId,sanswer) as row_num
FROM [dbo].[SingleProblemNewDetail]) t
pivot(max([SAnswer]) for row_num in ([1],[2],[3],[4])) pt) tbl1
inner join [dbo].[SingleProblemNew] tbl2 on tbl2.id=tbl1.[MainId]

posted @ 2017-02-24 13:40  恋之呓  阅读(101)  评论(0编辑  收藏  举报