数据库为sqlserver2005表Astudent type scores张三 语文 80张三 数学 70李四 语文 60李四 数学 90......要展示的效果为:学生名 语文 数学张三 80 70李四 60 90......要从这张表中得到这个展示效果的sql要怎么写?如果一张表不行,应该怎么加表?
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
DECLARE @sql VARCHAR(8000) set @sql ='select student AS [学生名] ' select @sql = @sql + ',MAX(CASE type WHEN '''+type+''' THEN scores END) ['+type+']' from (SELECT DISTINCT type FROM A ) as a exec(@sql+' from A group by student order by student desc')
ITMISS
TA贡献1871条经验 获得超8个赞
select a.student as 学生名,max(a.数学) as 数学 ,max(a.语文) as 语文 from (
select student ,' ' as 数学,scores as 语文 from 表A where type = '语文'
union
select student ,scores as 数学 ,' ' as 语文 from 表A where type = '数学'
) a
group by a.student
添加回答
举报
0/150
提交
取消