-- 示例数据
create table table1(id int, name varchar(10), sid int)
insert table1 select 1,'a',200
union all select 1,'b',652
union all select 1,'c',321
union all select 2,'d',241
union all select 2,'e',343
union all select 2,'g',898
union all select 1000,'x',136
union all select 1000,'v',8877
union all select 1000,'z',1233
go-- 查询
select id,
name1=max(case rid when 1 then name else '' end),
name2=max(case rid when 2 then name else '' end),
name3=max(case rid when 3 then name else '' end),
sid1=sum(case rid when 1 then sid else '' end),
sid2=sum(case rid when 2 then sid else '' end),
sid3=sum(case rid when 3 then sid else '' end)
from(
select *, rid=(select count(distinct name) from table1 where id=a.id and name<=a.name)
from table1 a
)aa
group by id
godrop table table1/*--结果
id name1 name2 name3 sid1 sid2 sid3
----------- ---------- ---------- ---------- ----------- ----------- -----------
1 a b c 200 652 321
2 d e g 241 343 898
1000 v x z 8877 136 1233(3 row(s) affected)--*/
create table table1(id int, name varchar(10), sid int)
insert table1 select 1,'a',200
union all select 1,'b',652
union all select 1,'c',321
union all select 2,'d',241
union all select 2,'e',343
union all select 2,'g',898
union all select 1000,'x',136
union all select 1000,'v',8877
union all select 1000,'z',1233
go-- 查询
select id,
name1=max(case rid when 1 then name else '' end),
name2=max(case rid when 2 then name else '' end),
name3=max(case rid when 3 then name else '' end),
sid1=sum(case rid when 1 then sid else '' end),
sid2=sum(case rid when 2 then sid else '' end),
sid3=sum(case rid when 3 then sid else '' end)
from(
select *, rid=(select count(distinct name) from table1 where id=a.id and name<=a.name)
from table1 a
)aa
group by id
godrop table table1/*--结果
id name1 name2 name3 sid1 sid2 sid3
----------- ---------- ---------- ---------- ----------- ----------- -----------
1 a b c 200 652 321
2 d e g 241 343 898
1000 v x z 8877 136 1233(3 row(s) affected)--*/
create table table1(id int, name varchar(10), sid int)
insert table1 select 1,'a',200
union all select 1,'b',652
union all select 1,'c',321
union all select 2,'d',241
union all select 2,'e',343
union all select 2,'g',898
union all select 1000,'x',136
union all select 1000,'v',8877
union all select 1000,'z',1233
go-- 查询
select id,
name1=max(case rid when 1 then name else '' end),
name2=max(case rid when 2 then name else '' end),
name3=max(case rid when 3 then name else '' end),
sid1=sum(case rid when 1 then sid else '' end),
sid2=sum(case rid when 2 then sid else '' end),
sid3=sum(case rid when 3 then sid else '' end)
from(
select *, rid=row_number() over(partition by id order by name)
from table1 a
)aa
group by id
godrop table table1/*--结果
id name1 name2 name3 sid1 sid2 sid3
----------- ---------- ---------- ---------- ----------- ----------- -----------
1 a b c 200 652 321
2 d e g 241 343 898
1000 v x z 8877 136 1233(3 row(s) affected)--*/
update a set nid=(select count(*) from #t where id=a.id and rid<=a.rid) from #t aselect
id,
name1=max(case nid when 1 then name end),
name2=max(case nid when 2 then name end),
name3=max(case nid when 3 then name end),
sid1 =max(case nid when 1 then sid end) ,
sid2 =max(case nid when 2 then sid end) ,
sid3 =max(case nid when 3 then sid end)
from
#t
group by id order by id
(id int,name varchar(20),sid int)insert t
select 1,'a',200 union all
select 1,'b',652 union all
select 1,'c',321 union all
select 2,'d',241 union all
select 2,'e',343 union all
select 2,'g',898declare @sql varchar(8000)
select @sql='select id'
select @sql=@sql+',[name'+cast(t_id as varchar)+']=
max(case when t_id='+cast(t_id as varchar)+' then name end)'
from (select t_id=(select count(1) from t a where a.id=b.id and a.name<=b.name),* from t b) m group by t_idselect @sql=@sql+',[sid'+cast(t_id as varchar)+']=
max(case when t_id='+cast(t_id as varchar)+' then sid end)'
from (select t_id=(select count(1) from t a where a.id=b.id and a.name<=b.name),* from t b) m group by t_idselect @sql=@sql+'
from (select t_id=(select count(1) from t a
where a.id=b.id and a.name<=b.name),* from t b) m group by id'exec(@sql)
drop table t