Mysql取分组中前N条记录

表结构如下:
CREATE TABLE `dwb_rmirror_req_d` (
`thedate` varchar(10) NOT NULL DEFAULT ‘‘,
`node` varchar(15) NOT NULL DEFAULT ‘‘,
`req_num` bigint(20) DEFAULT NULL,
PRIMARY KEY (`thedate`,`node`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

表中的记录如下:
mysql> select * from dwb_rmirror_req_d;
+----------+------+------------+
| thedate | node | req_num |
+----------+------+------------+
| 20160215 | f | 2 |
| 20160215 | i | 1 |
| 20160215 | l | 3 |
| 20160217 | f | 2 |
| 20160217 | i | 1 |
| 20160217 | l | 3 |
| 20160218 | f | 2 |
| 20160218 | i | 1 |
| 20160218 | l | 3 |
| 20160219 | f | 2 |
| 20160219 | i | 1 |
| 20160219 | l | 3 |
| 20160220 | f | 2 |
| 20160220 | i | 1 |
| 20160220 | l | 3 |
| 20160221 | f | 2 |
| 20160221 | i | 1 |
| 20160221 | l | 3 |
+----------+------+------------+
18 rows in set (0.00 sec)

1.获取每天查询量最大的记录:
select a.thedate,a.node,a.req_num from dwb_rmirror_req_d a left join dwb_rmirror_req_d b
on a.thedate = b.thedate and a.req_num <= b.req_num
group by a.thedate,a.node,a.req_num
having count(b.node)<=1;
结果如下:
+----------+------+------------+
| thedate | node | req_num |
+----------+------+------------+
| 20160215 | l | 3 |
| 20160217 | l | 3 |
| 20160218 | l | 3 |
| 20160219 | l | 3 |
| 20160220 | l | 3 |
| 20160221 | l | 3 |
+----------+------+------------+
6 rows in set (0.01 sec)

2. 获取每天查询量最高的两条记录:
select a.thedate,a.node,a.req_num from dwb_rmirror_req_d a left join dwb_rmirror_req_d b
on a.thedate = b.thedate and a.req_num <= b.req_num
group by a.thedate,a.node,a.req_num
having count(b.node)<=2
order by a.thedate,a.req_num;
结果如下:
+----------+------+------------+
| thedate | node | req_num |
+----------+------+------------+
| 20160215 | f | 2 |
| 20160215 | l | 3 |
| 20160217 | f | 2 |
| 20160217 | l | 3 |
| 20160218 | f | 2 |
| 20160218 | l | 3 |
| 20160219 | f | 2 |
| 20160219 | l | 3 |
| 20160220 | f | 2 |
| 20160220 | l | 3 |
| 20160221 | f | 2 |
| 20160221 | l | 3 |
+----------+------+------------+
12 rows in set (0.01 sec)

时间: 2024-08-25 19:32:28

Mysql取分组中前N条记录的相关文章

Mysql 高效随机生成N条记录

1 select *, rand() as random FROM yef_exercises where id not in(1) order by random limit 1 // Mysql Mysql 高效随机生成N条记录,布布扣,bubuko.com

[mysql] 先按某字段分组再取每组中前N条记录

From: http://blog.chinaunix.net/uid-26729093-id-4294287.html 请参考:http://bbs.csdn.net/topics/330021260 create table t2 (    id int primary key,    gid    char,    col1    int,    col2    int) engine=myisam; insert into t2 values (1,'A',31,6),(2,'B',25

分区函数Partition By的与row_number()的用法以及与排序rank()的用法详解(获取分组(分区)中前几条记录)(转)

转载地址:http://www.cnblogs.com/linJie1930906722/p/6036053.html partition by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition by用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组,分区函数一般与排名函数一起使用. 准备测试数据: create table Student --学生成绩表 ( id int, --主键 Grad

MySQL 快速构造一亿条记录的表

  在上一次朋友问我如何快速构造一亿条记录的表后,我理出了实行的办法,见:http://blog.csdn.net/mchdba/article/details/52938114,但是因为录入一亿表需要2个小时,所以还是感觉速度慢了些,那有没有啥办法能加快这一步骤呢?   1.建一张通用的用户表 建用户表没有啥变化,还是和上次一样. USE test; CREATE TABLE `UC_USER` ( `ID` BIGINT (20), `USER_NAME` VARCHAR (400), `U

MySQL 如何准备一亿条记录的表来测试

曾经一个朋友问我如何快速的在线往一个大表里面添加一个字段或者修改一个字段的长度,mysql版本是5.6,所以就准备在测试环境准备一个一亿条记录的表,然后来实际测试下到底哪种方式比较快,先来开始准备一亿条记录的表.   我线上有上亿条记录的表,但是很多网上朋友都没有,那么我这里就实践了一条办法,来实现自己构造一亿条数据记录的表.实现思路就是先建一张通用的20个字段左右的用户表,然后写一个存储过程,不停的往这个表里面写数据,while循环写上一亿次,这样就形成了一张一亿条记录的表出来.     1.

mysql查询表中最后一条记录

查询全部的记录:            select * from test_limit ; 查第一条记录:             select * from test_limit limit 1; 查前面两条记录:         select * from test_limit limit 0,2; 查第二和第三条记录:     select * from test_limit limit 1,2; 查最后一条记录:          select * from test_limit or

oracle 如何随机取表中n条记录?

一.使用dbms_random.value进行排序 执行结果如下 第一次执行结果 第二次执行结果 第三次执行结果 经过三次试验,取到的数都是随机的 原文地址:http://blog.51cto.com/4923168/2090149

sql分组获取第一条记录(sql+oracle)

sql版本 select * from (select t.CloseDate,t.ExpiryDate,t.DataTypeLookupID,ROW_NUMBER() over(partition by CloseDate,ExpiryDate,DataTypeLookupID order by CloseDate,ExpiryDate,DataTypeLookupID) as new_index from dbo.IndexVolatilityMarketData t ) a where a

ResultSet取结果集多少条记录方法及分页概要

allst=toconn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); allsql = "SELECT distinct docid,docchannel FROM isimportant where docchannel='"+CHANNELID+"'"; allrs=allst.executeQuery(allsql); allrs.last();