Group fetch top N Records

Source: Internet
Author: User

group fetch top N Records 

Often see the problem, how to take out the first N records of each group. Convenient for everyone to refer to the common several solutions listed below.

question: The table below, requires the removal of the top two classes (allow second)
Table1
+----+------+------+-----+
| ID | SName | Clsno | score|
+----+------+------+-----+
| 1 |  AAAA | C1 | 67 |
| 2 |  BBBB | C1 | 55 |
| 3 |  CCCC | C1 | 67 |
| 4 |  DDDD | C1 | 65 |
| 5 |  eeee | C1 | 95 |
| 6 |  FFFF | C2 | 57 |
| 7 |  GGGG | C2 | 87 |
| 8 |  HHHH | C2 | 74 |
| 9 |  IIII | C2 | 52 |
| 10 |  JJJJ | C2 | 81 |
| 11 |  KKKK | C2 | 67 |
| 12 |  llll | C2 | 66 |
| 13 |  MMMM | C2 | 63 |
| 14 |  NNNN | C3 | 99 |
| 15 |  oooo | C3 | 50 |
| 16 |  PPPP | C3 | 59 |
| 17 |  QQQQ | C3 | 66 |
| 18 |  RRRR | C3 | 76 |
| 19 |  SSSS | C3 | 50 |
| 20 |  TTTT | C3 | 50 |
| 21 |  uuuu | C3 | 64 |
| 22 |  VVVV | C3 | 74 |
+----+------+------+-----+
The results are as follows
+----+------+------+-----+
| ID | SName | Clsno | score|
+----+------+------+-----+
| 5 |  eeee | C1 | 95 |
| 1 |  AAAA | C1 | 67 |
| 3 |  CCCC | C1 | 67 |
| 7 |  GGGG | C2 | 87 |
| 10 |  JJJJ | C2 | 81 |
| 14 |  NNNN | C3 | 99 |
| 18 |  RRRR | C3 | 76 |
+----+------+------+-----+

Method One:
Select A.id,a.sname,a.clsno,a.score
From Table1 a LEFT join Table1 B on A.clsno=b.clsno and A.score<b.score
GROUP BY A.id,a.sname,a.clsno,a.score
Having count (b.id) <2
ORDER BY A.clsno,a.score Desc

Method Two:

SELECT *
From Table1 A
where 2> (select COUNT (*) from Table1 where Clsno=a.clsno and Score>a.score)
ORDER BY A.clsno,a.score Desc

Method Three:
SELECT *
From Table1 A
where ID in (select ID from Table1 where clsno=a.clsno order BY score desc limit 2)
ORDER BY A.clsno,a.score Desc

Method....

Here are a list of implementations of various SQL statements, some of which are MySQL-specific (Limit, other databases can be based on actual changes, such as Oracle's ROWNUM,MS SQL SERVER top,..), sometimes supported by the SQL standard. But the efficiency and application of the occasion may be different. Specific application can be based on the actual table records, the index of the situation to choose.

Special case N=1, that is, take the largest/smallest record.
+----+------+------+-----+
| ID | SName | Clsno | score|
+----+------+------+-----+
| 5 |  eeee | C1 | 95 |
| 7 |  GGGG | C2 | 87 |
| 14 |  NNNN | C3 | 99 |
+----+------+------+-----+

SELECT *
From Table1 A
Where NOT EXISTS (select 1 from Table1 where Clsno=a.clsno and Score>a.score);

Select A.*
From Table1 a inner joins (select Clsno, Max (score) as Mscore from Table1 Group by Clsno) b
On A.clsno=b.clsno and A.score=b.score

SELECT *
From (SELECT * from Table1 ORDER BY score desc) T
GROUP BY Clsno

Group fetch top N Records

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.