The data table is as follows:
I'm using a MySQL database, so the relevant SQL is
- CREATE TABLE ' Stuscore ' (
- ' ID ' int (one) not NULL auto_increment,
- 'name ' varchar ( default NULL),
- ' Subject ' varchar default NULL,
- ' Score ' varchar default NULL,
- ' Stuid ' varchar default NULL,
- PRIMARY KEY (' id ')
- ) Engine=innodb DEFAULT Charset=utf8;
- /*data for the table ' stuscore ' * /
- Insert INTO ' Stuscore ' (' id ', 'name ', ' Subject ', ' score ', ' stuid ') values
- (1,' Zhang San ',' math ',' the '1 '),
- (2,' Zhang San ',' language ', ' a ', '1 '),
- (3,' Zhang San ',' English ',' all ',' 1 '),
- (4,' John Doe ',' math ',' all ',' 2 '),
- (5,' John Doe ',' language ','2 ') ,
- (6,' John Doe ',' English ',' n ',' 2 ');
The following are the related questions:
1. Calculate the total number of each person and rank (requires display field: Name, overall scores)
2. Calculate each person's total and rank (required field: number, name, overall)
3. Calculate the highest score for each individual (required field: School number, name, course, highest score)
4. Calculate each person's average score (requires display field: study number, name, average score)
5. List the students who have the best grades in each course (Required fields: Student number, name, subject, score)
6. List of the two students who have the best results in each course (Required fields: Student number, name, subject, score)
7. The statistics are as follows:
5. List the students who have the best grades in each course (Required fields: Student number, name, subject, score)
6. List of the two students who have the best results in each course (Required fields: Student number, name, subject, score)
8. List the average scores for each course (Required fields: course, average)
9. List the ranking of math scores (required fields: School number, name, score, rank)
10. List of students with a mathematical score of 2-3 (Requirements display field: Student number, name, subject, score)
11. Find out the ranking of John Doe's mathematical achievements
13. The statistics are as follows: Mathematics: Zhang San (50 points), John Doe (90 points), Harry (90 points), Zhao Liu (76 points)
The answers are as follows:
1. Calculate the total number of each person and rank (requires display field: Name, overall scores)
- Select name,sum(score) as Allscore from stuscore group by name order by al Lscore desc
2. Calculate each person's total and rank (required field: number, name, overall)
- Select Stuid,name,sum(score) as Allscore from stuscore group by name order C11>by allscore desc
3. Calculate the highest score for each individual (required field: School number, name, course, highest score)
- SELECT t1.stuid,t1. Name,t1.subject,t1.score from stuscore T1,
- (SELECT Stuid,Max (score) as Maxscore from stuscore Group by stuid) T2
- where T1.stuid=t2.stuid and T1.score=t2.maxscore
4. Calculate each person's average score (requires display field: study number, name, average score)
- Select distinct t1.stuid,t1. Name,t2.avgscore from stuscore T1,
- (Select Stuid,avg(Score) as Avgscore from stuscore Group by stuid) T2
- where T1.stuid=t2.stuid
5. List the students who have the best grades in each course (Required fields: Student number, name, subject, score)
- Select T1.stuid,t1. Name,t1.subject,t2.maxscore from stuscore T1,
- (Select Subject,max (score) as Maxscore from stuscore Group by subject) T2 /c5>
- where T1.subject=t2.subject and T1.score=t2.maxscore
6. List of the two students who have the best results in each course (Required fields: Student number, name, subject, score)
- Select distinct t1.* from stuscore T1 where T1.stuid in
- (Select Top 2 stuscore.stuid from stuscore where subject = T1.subject ORDER by score desc)
- ORDER BY T1.subject
The statement is not tested and is now known to be unable to run under MySQL. MySQL does not support select top.
7.
- Select Stuid as school number,name as name,
- sum (Case when subject=' language ' then score else 0 end) as language, /c9>
- sum (Case when subject=' math ' then score else 0 end) as math,
- sum (Case when subject=' English ' then score else 0 end) as English,
- sum (score) as Score, (sum(score)/Count(*)) as average
- From Stuscore
- Group by Stuid,name
- ORDER by Total desc
8. List the average results of each course (required field: course, average)
- Select Subject,avg(Score) as Avgscore from stuscore GROUP by subject
9. List the ranking of math scores (Required fields: study number, name, score, rank)
- SELECT * from stuscore where subject =' math ' order by score desc
I don't know what I think, right?
10. List of students with a math score of 2-3 (Requirements display field: Student number, name, subject, score)
- Select t3.* from (
- Select Top 2 t2.* from (
- Select Top 3 name,subject,score,stuid from stuscore where subject=' math '
- ORDER by score desc
- ) T2 ORDER by T2.score
- ) T3 ORDER by T3.score desc
11. Find out the ranking of John Doe's mathematical achievements
- DECLARE @tmp table (PM int,name varchar (score), int,stuid int)
- Insert INTO @tmp select null,name,score,stuid from stuscore where subject=' math ' ORDER by score desc
- DECLARE @id int
- Set @id = 0;
- Update @tmp set @[email protected]+1,[email protected]
- SELECT * from @tmp where name=' John Doe '
12.
- Select Subject,
- (Select Count(*) from Stuscore where score<60 and subject=t1.subject) as failed,
- (Select Count(*) from Stuscore where score between and Subject=t1.subject) as good,
- (Select Count(*) from Stuscore where score >80 and subject= T1.subject) as excellent
- From Stuscore T1 GROUP by subject
13. The statistics are as follows: Mathematics: Zhang San (50 points), John Doe (90 points), Harry (90 points), Zhao Liu (76 points)
- DECLARE @s varchar (+)
- Set @s="
- Select @s [Email protected]+', ' +name+' (' +convert(varchar), score) +') ' From Stuscore where subject=' mathematics '
- Set @s=stuff (@s,1,1,")
- Print ' math: ' [email protected]
Note: section 10,11,13 is not verified.
Original from: http://www.cnblogs.com/tenghoo/archive/2007/06/11/779240.htm
Good text to the top of my collection of the article Pato '
Follow-6
Fans-31 + plus attention
sql--Statistical Query