将表:
ID 字段 值(字符串型)
-----------------------
A 高度 1
A 长度 2
A 宽度 3
B 高度 4
B 长度 5
B 宽度 6转换成如下:ID 高度 长度 宽度
-----------------------
A 1 2 3
B 4 5 6
请高手指教!谢谢!
ID 字段 值(字符串型)
-----------------------
A 高度 1
A 长度 2
A 宽度 3
B 高度 4
B 长度 5
B 宽度 6转换成如下:ID 高度 长度 宽度
-----------------------
A 1 2 3
B 4 5 6
请高手指教!谢谢!
2 2005001 数学 90.0
3 2005001 英语 80.0
4 2005002 语文 56.0
5 2005002 数学 69.0
6 2005002 英语 89.0执行select sid,语文=isnull(sum(case course when '语文' then result end),0),
数学=isnull(sum(case course when '数学' then result end),0),
英语=isnull(sum(case course when '英语' then result end),0)
from result
group by sid
order by sid 得出结果sid 语文 数学 英语 2005001 80.0 90.0 80.0
2005002 56.0 69.0 89.0 2)较为复杂的行转列表1:courseid name1 语文
2 数学
3 英语
表2:resultid sid course result1 2005001 语文 80.0
2 2005001 数学 90.0
3 2005001 英语 80.0
4 2005002 语文 56.0
5 2005002 数学 69.0
6 2005002 英语 89.0 declare @sql varchar(8000)
set @sql='select sid'
select @sql=@sql+','+course.name+'=isnull(sum(case course when '''+course.name+''' then result end),0)'
from course order by id
set @sql=@sql+' from result group by sid order by sid'
print @sql
exec(@sql) 得出结果sid 语文 数学 英语 2005001 80.0 90.0 80.0
2005002 56.0 69.0 89.0新闻来自: 新客网(www.xker.com) 详文参考:http://www.xker.com/page/e2007/0205/22415.html
max(case 字段 when '高度' then 值 else 0 end) 高度,
max(case 字段 when '长度' then 值 else 0 end) 长度,
max(case 字段 when '宽度' then 值 else 0 end) 宽度
from tb
group by ID
select
id, (case when 字段='长度' then 值 else 0 end) as 长度,
(case when 字段='宽度' then 值 else 0 end) as 宽度,
(case when 字段='高度' then 值 else 0 end) as 高度
from table group by id order by id