Table row and column transpose (Database Common test)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.