有一个表table1
id name 科目 成绩
1 a chinese 56
2 b chinese 45
3 a english 65
4 b english 87我想得到这样的表name chinese english
a 56 45
b 45 87
怎么写。
id name 科目 成绩
1 a chinese 56
2 b chinese 45
3 a english 65
4 b english 87我想得到这样的表name chinese english
a 56 45
b 45 87
怎么写。
解决方案 »
- SQL2008数据库损毁怎样还原数据
- (初学)用sql server创建了登录名,却登录不进去
- 请教关于reporting builder 使用
- 请教一下关于 2008 Express R2 的报表服务器问题。
- --急:考勤问题,求更好的方法?--
- 如何把SQL语句写成一个批处理文件?
- 存储过程中,如何把order by,where ,整条作为参数!
- 大量INSERT语句插入数据库 求解决方案(XML写入数据库的会的也请进来看下)
- ╋╋╋╋╋╋╋Visual c++远程访问sql数据库问题,请关注!!!╋╋╋╋╋╋╋
- sql server触发器批量更新只对第一条记录起作用是怎么回事
- SQL SERVER2005的一个奇怪问题,急,急,急!!!!!
- 急!! 如何让SQL SERVER 2005 登录名不区分大小写?在哪里可以设置.
english = max(case 科目 when 'engligh' then 成績 else null end)
from table1
group by name
select @s = isnull(@s, '') + ', max(case 科目 when ''' + 科目 + ''' then 成績 else null) as [' + 科目 + ']'
from table1
set @s = stuff(@s, 1, 1, '')
set @s = 'select name,' + @s + ' from table1 group by name'exec(@s)
你的第二个方法好象过不去
select @s = isnull(@s, '') + ', max(case 科目 when ''' + 科目 + ''' then 成績 else null end) as [' + 科目 + ']'
from table1
set @s = stuff(@s, 1, 1, '')
set @s = 'select name,' + @s + ' from table1 group by name'exec(@s)------------
剛才case語句掉了end
name chinese mathes maths chinese English English chinese
hua 85 NULL NULL 85 NULL NULL 85
tai 52 78 78 52 95 95 52
wei 84 62 62 84 76 76 84有重复的
select @s = isnull(@s, '') + ', max(case 科目 when ''' + 科目 + ''' then 成績 else null end) as [' + 科目 + ']'
from table1
group by 科目 --這裏加上
set @s = stuff(@s, 1, 1, '')
set @s = 'select name,' + @s + ' from table1 group by name'exec(@s)
那个max()是什么意思