The first time you write a blog, it's all about the database.
The table is as follows: (table name WFGTEST1,WFG my nickname)
| NAME |
COURSE |
GRADE |
| Wfg |
Mathematical |
90 |
| Wfg |
English |
80 |
| Wfg |
Chinese |
100 |
| Lxx |
Chinese |
100 |
| Lxx |
Mathematical |
90 |
| Lxx |
English |
80 |
| Wly |
Chinese |
100 |
| Wly |
Mathematical |
90 |
| Wly |
English |
80 |
Table 1
This watch is very good-looking, but also our college STUDENT,COURSE,SC often save the table, the question came, how to convert this table to the following table?
| NAME |
language |
Mathematics |
English |
| Wly |
100 |
90 |
80 |
| Lxx |
100 |
90 |
80 |
| Wfg |
100 |
90 |
80 |
Table 2
Use the following SQL can be done!!!
1 Selectname,2 sum( CaseCOURSE when 'language' ThenGRADEElse 0 End) aslanguages,3 sum( CaseCOURSE when 'Mathematics' ThenGRADEElse 0 End) asMathematics,4 sum( CaseCOURSE when 'English' ThenGRADEElse 0 End) asEnglish5 fromWfgtest16 Group byName
The general problem is this, now convert a train of thought, how to change table 2 to table 1?
This event tells us to be able to lift a three. Get in and get out.
And then the code.
1 SelectName'language' asCourse, Chinese asGrade fromWfgtest1Union All 2 SelectName'Mathematics' asCourse, Math asGrade fromWfgtest1Union All 3 SelectName'English' asCourse, English asGrade fromWfgtest1
Pro: 2015 The first blog post, this blog post is a small step for cnblogs, but it is a big step for me personally!
Come on!
Table row and column transpose (Database Common test)