Find the first n records of each group in SQL

Source: Internet
Author: User

Query the first n articles of a specified category

CREATE TABLE ' person '
(
' Person ' varchar (10),
' Group ' int,
' Age ' int
);

insert INTO ' person ' values
(' Bob ', 1, 32),
(' Jill ', 1, 34),
(' Shawn ', 1, 42),
(' Jake ', 2, 29),
(' Paul ', 2, 36),
(' Laura ', 2, 39);


Query statement
Select A.person, A.group, A.age from Who as a where (SELECT COUNT (*) from person as B where B.group = A.group and B.age >= a.age) <= 2 order by A.group ASC, A.age DESC


SQL: Get the same sortid result set, each ID three data

A table:

SQL code

CREATE TABLE ' book ' (
' BookID ' int (one) unsigned not NULL auto_increment,
' SortID ' int (one) is not NULL,
' BookName ' varchar (255) Not NULL DEFAULT ',
PRIMARY KEY (' BookID ')
) Engine=innodb auto_increment=80729 DEFAULT Charset=utf8;

The data is like this:

xml/html Code
Insert INTO ' book ' (' BookID ', ' sortid ', ' bookname ') VALUES (1, 1, ' 1 '), (2,1, ' 2 '), (3,1, ' 3 '), (4,1, ' 4 '), (5,2, ' 1 '), (6,2, ' 2 ') ), (7,2, ' 3 '), (8,2, ' 4 '), (9,2, ' 5 '), (10,2, ' 6 '), (11,2, ' 7 '), (12,2, ' 8 '), (13,2, ' 9 '), (14,2, ' 10 '), (15,2, ' 11 '), (16,3, ' 1 ') , (17,3, ' 2 '), (18,3, ' 3 '), (19,3, ' 4 '), (20,3, ' 5 '), (21,3, ' 6 '), (22,3, ' 7 '), (23,3, ' 8 '), (24,3, ' 9 '), (25,3, ' 10 '), ("26,3"), 11 '), (27,3, ' 12 '), (28,4, ' 1 '), (29,4, ' 2 '), (30,4, ' 3 '), (31,4, ' 4 '), (32,4, ' 5 '), (33,5, ' 1 '), (34,5, ' 2 '), (35,5, ' 3 '), (() 36,5 , ' 4 ');

In this case, to obtain SortID in (1,2,3) data, the result of each sortid is 3.

This topic is a lot of online, but there is really no special good SQL, there are many kinds of online. At first, I wanted to do it by union, but eventually it was so bad that my colleague clear made the SQL good and tried it, and in the tens of thousands of data, it worked, and the SQL was relatively simple:

SQL code


Select Sortid,bookname from book B where SortID into (1,2) and 3> (select COUNT (1) from the book WHERE B.sortid=sortid and book Id<b.bookid)


Table autocorrelation, indexing on the sortid is a good efficiency


How to select the First/least/max row/group in SQL

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.