一个貌似挺复杂的sql语句问题!多谢回答啊啊啊!!
有一张数据表 表结构如下
id	name	value	class
1	我	11	水
2	我	12	气
3	你	21	水
4	他	31	水
5	他	32	气
6	她	41	水
现在想通过sql语句来获得这样的结构
id	name	水value	气value
1	我	11	22
2	你	21	null
3	他	31	32
4	她	41	null
也就是说根据class来将表结构变了,谁能帮解决问题或给个提示,多谢啦!
              
------解决方案--------------------select 
    row_number as id, 
    name, 
    sum(case when class='水' then value else 0 end) as 水类, 
    sum(case when class='气' then value else 0 end) as 气类 
from 
    table 
group by 
    name
------解决方案--------------------create table #tb(id int,name varchar(10),value int,class varchar(10))
insert into #tb
select 1,'我',11,'水'
union all select 2,'我',12,'气'
union all select 3,'你',21,'水'
union all select 4,'他',31,'水'
union all select 5,'他',32,'气'
union all select 6,'她',41,'水'
select *
from 
(
select *,id=row_number() over(order by [水value])
from (
	select name,[水value]=max(case when class='水' then value end),[气value]=max(case when class='气' then value end)
	from #tb
	group by name
	)t
)a
order by id
/*
id	name	水value	气value
---------------------------------
1	我	11	12
2	你	21	NULL
3	他	31	32
4	她	41	NULL
*/
------解决方案-------------------- 
楼上的都正确
select 
    row_number as id, 
    name, 
    sum(case when class='水' then value else 
null end) as 水类, 
    sum(case when class='气' then value else 
null end) as 气类 
from 
    table 
group by 
    name