(现在刚学数据库,看到一个题目,自己先做了下,用了三个temp table,很笨拙的方法. 后来才知道这个是 “行转列的问题”,有很简洁的代码,不过不管怎么说,先贴下自己的方法,毕竟是自己辛辛苦苦写的吗,hehe... )
问题:
ID 姓名 科目 分数
01 a 语文 80
01 a 数学 70
01 a 英语 80
02 b 语文 80
02 b 数学 70
02 b 英语 80
怎样用SQL查出
ID 姓名 语文 数学 英语
01 a 80 70 80
02 b 80 70 80
way1 (of myself:)
Description | Create three temp tables to store every score apart, then join three table |
Code |
--Create Test DB --Create Table --Insert sample data --Check the input --Create three temp table Insert Into #ChineseScore Select * from #ChineseScore ---Math Temp Table Insert Into #MathScore Select * from #MathScore ---English Temp Table Insert Into #EnglishScore Select * from #EnglishScore --Get final result |