SQLServer2008 行转列

SQLServer2008 行转列_第1张图片SQLServer2008 行转列_第2张图片

with a as(
select *,row_number() over(partition by hyid order by jp desc) rowid
from rtc)
select a.hyid,
	max(case when a.rowid=1 then a.mc END) mc,
	max(case when a.rowid=1 then a.jp END) jp,

	max(case when a.rowid=2 then a.mc END) mc2,
	max(case when a.rowid=2 then a.jp END) jp2,

	max(case when a.rowid=3 then a.mc END) mc3,
	max(case when a.rowid=3 then a.jp END) jp3,

	max(case when a.rowid=4 then a.mc END) mc4,
	max(case when a.rowid=4 then a.jp END) jp4,

	max(case when a.rowid=5 then a.mc END) mc5,
	max(case when a.rowid=5 then a.jp END) jp5
from a
group by a.hyid

 

你可能感兴趣的:(sqlserver)