高分,怎么实现这个一个多行转一行的查询

高分求助,如何实现这个一个多行转一行的查询?
现在这样一个表:

ID      SIDCard Name Code CodeName
1179 12345 张三 2B 工程造价
1180 12345 张三 3B 建筑工程
1181 12345 张三 4c 建筑AB

希望能根据SIDCard转为一行
SIDCard Name Code1 codeName1 Code2 codeName2 Code2 codeName3
1345 张三 2B 工程造价 3B 建筑工程   4c 建筑AB

当然,有可能每个身份证行数在1-3行之间,请问如何实现这样的T_SQL查询?


------解决思路----------------------

create table mn
(ID int,SIDCard int,Name varchar(10),Code varchar(10),CodeName varchar(20))

insert into mn
 select 1179,12345,'张三','2B','工程造价' union all
 select 1180,12345,'张三','3B','建筑工程' union all
 select 1181,12345,'张三','4c','建筑AB' union all      -- 3行
 select 1182,12346,'李四','5d','建筑设计' union all
 select 1183,12346,'李四','6e','施工管理' union all    -- 2行
 select 1184,12347,'王五','7f','AutoCAD'               -- 1行


declare @tsql varchar(6000)

select @tsql=isnull(@tsql+',','')
            +'max(case when rn='+rtrim(number)+' then Code else '''' end) ''Code'+rtrim(number)+''','
            +'max(case when rn='+rtrim(number)+' then CodeName else '''' end) ''CodeName'+rtrim(number)+''' '
 from master.dbo.spt_values
 where type='P' 
 and number between 1 and 
 (select max(cnt) from (select count(1) 'cnt'
                        from mn 
                        group by SIDCard,Name) t)

select @tsql='select SIDCard,Name,'+@tsql
            +' from (select *,row_number() over(partition by SIDCard,name order by Code) ''rn'' from mn) t '
            +' group by SIDCard,Name '
            +' order by SIDCard '

exec(@tsql)

/*
SIDCard     Name       Code1      CodeName1            Code2      CodeName2            Code3      CodeName3
----------- ---------- ---------- -------------------- ---------- -------------------- ---------- --------------------
12345       张三         2B         工程造价             3B         建筑工程                 4c         建筑AB
12346       李四         5d         建筑设计             6e         施工管理                            
12347       王五         7f         AutoCAD                                                         

(3 行受影响)
*/

------解决思路----------------------
看了版主的回复,是我理解错需求啦,请忽略我的答案高分,怎么实现这个一个多行转一行的查询
------解决思路----------------------
不使用动态sql,应该可以写成函数或视图:

;with t as (
select (ROW_NUMBER() over(partition by SIDCard order by Code)) fid,* from tb
)
select a.SIDCard,a.Name
,a.code Code1,a.codename codeName1
,b.code Code2,b.codename codeName2
,c.code Code3,c.codename codeName3
from t t1
left join t b on b.fid=2 and a.SIDCard=b.SIDCard
left join t c on c.fid=3 and a.SIDCard=c.SIDCard
where a.fid=1