SQL b table is sorted by a table ID sum

Source: Internet
Author: User

The SQL statement that identifies the student's grades and overall scores and the total score according to the person's name

/*

CREATE TABLE Student Table (

Study number nvarchar (+) NULL,

Name nvarchar (+) null)

CREATE TABLE Score Table (

Study number nvarchar (+) NULL,

Account nvarchar (+) NULL,

Score decimal (10,1) null

)

INSERT INTO student table values (' 101 ', ' Zhang San ')

INSERT INTO student table values (' 104 ', ' John Doe ')

INSERT INTO student table values (' 107 ', ' Harry ')

INSERT INTO student table values (' 111 ', ' any six ')

Insert INTO score table values (' 101 ', ' language ', 90)

Insert INTO score table values (' 101 ', ' Math ', 91)

Insert INTO score table values (' 101 ', ' English ', 92)

Insert INTO score table values (' 101 ', ' Politics ', 93)

Insert INTO score table values (' 104 ', ' language ', 96)

Insert INTO score table values (' 104 ', ' math ', 70)

Insert INTO score table values (' 104 ', ' English ', 80)

Insert INTO score table values (' 104 ', ' politics ', 92)

Insert INTO score table values (' 107 ', ' language ', 70)

Insert INTO score table values (' 107 ', ' math ', 100)

Insert INTO score table values (' 107 ', ' English ', 90)

Insert INTO score table values (' 107 ', ' Politics ', 98)

Insert INTO score table values (' 111 ', ' language ', 97)

Insert INTO score table values (' 111 ', ' math ', 90)

Insert INTO score table values (' 111 ', ' English ', 99)

Insert INTO score table values (' 111 ', ' Politics ', 99)

*/

Select A. Name

, (select B. score from table B where b. Subject = ' language ' and A. Study number =b) as language

, (select B. score from score table B where b. Subject = ' mathematics ' and A. =b No.) as Mathematics

, (select B. score from score table B where b. Subject = ' English ' and A. Study number =b) as English

, (select B. score from score table B where b. Subject = ' politics ' and A. =b number) as politics

, (select SUM (b. Score) from the score table B where A. Study number =b.) as Total

From student table A order by Total DESC

Query Result:

SQL b table is sorted by a table ID sum

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.