文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

首先建表:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `fruit`
-- ----------------------------
DROP TABLE IF EXISTS `fruit`;
CREATE TABLE `fruit` (
`type` varchar(10) NOT NULL,
`variety` varchar(20) NOT NULL,
`price` double(10,2) NOT NULL,
PRIMARY KEY (`type`,`variety`),
KEY `type` (`type`,`price`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of fruit
-- ----------------------------
INSERT INTO `fruit` VALUES ('apple', 'fuji', '0.24');

INSERT INTO `fruit` VALUES ('apple', 'gala', '2.79');

INSERT INTO `fruit` VALUES ('apple', 'limbertwing', '2.87');

INSERT INTO `fruit` VALUES ('apple', 'seswe', '3.84');

--------------------------------------------------------------------

INSERT INTO `fruit` VALUES ('cherry', 'bing', '2.55');

INSERT INTO `fruit` VALUES ('cherry', 'chelan', '6.33');

INSERT INTO `fruit` VALUES ('cherry', 'drtan', '7.33');

INSERT INTO `fruit` VALUES ('cherry', 'drtyd', '7.87');

---------------------------------------------------------------------

INSERT INTO `fruit` VALUES ('orange', 'valencia', '3.59');

INSERT INTO `fruit` VALUES ('orange', 'navel', '9.36');

INSERT INTO `fruit` VALUES ('orange', 'vcxss', '9.43');

INSERT INTO `fruit` VALUES ('orange', 'vbng', '11.33');

INSERT INTO `fruit` VALUES ('orange', 'cccbn', '21.36');

--------------------------------------------------------------------

INSERT INTO `fruit` VALUES ('pear', 'bartlett', '2.14');

INSERT INTO `fruit` VALUES ('pear', 'bradford', '6.05');

INSERT INTO `fruit` VALUES ('pear', 'ddssa', '8.54');

INSERT INTO `fruit` VALUES ('pear', 'rtyug', '9.07');

然后希望得到每种水果中价格第二高的数据,即:

+--------+------------+-------+
| type | variety | price |
+--------+------------+-------+
| apple | limbertwig | 2.87 |
| cherry | drtan | 7.33 |
| orange | vbng | 11.33 |
| pear | ddssa | 8.54 |
+--------+------------+-------+ 参考如下sql语句:

SELECT t.type,MIN(t.price) price FROM(SELECT
    a.type,a.price
    FROM fruit a
    WHERE
      2 >= (
          SELECT
          COUNT(*)
          FROM
          fruit b
          WHERE
          a.type= b.type
          AND a.price <= b.price
         )
    ORDER BY
    a.type,
    a.price)t

GROUP BY type

本sql使用了两重子查询,效率较低。请高人指点~

mysql先分组,然后取每个分组中的第2大的记录的更多相关文章

  1. oracle分组后取某组中最大的值

    查询username,根据fundcode分组,按照date倒序,取date最大的一条数据 select * from ( select username, row_number() over(par ...

  2. Excel—分组然后取每组中对应时间列值最大的或者最小的

    1.MAX(IF(A:A=D2,B:B)) 输入函数公式后,按Ctrl+Shift+Enter键使函数公式成为数组函数公式. Ctrl+Shift+Enter: 按住Ctrl键不放,继续按Shift键 ...

  3. 基于mysql实现group by取各分组最新一条数据

    准备数据 SQL语句 SELECT * FROM admin WHERE id IN ( SELECT MAX( id ) FROM admin GROUP BY order_id ); 查询结果:

  4. group by分组后获得每组中符合条件的那条记录

    当group by单独使用时,只显示出每组的第一条记录.如下,未分组时查询出两条记录 SELECT info.id, info.switch_id, info.port_id, info.mac_ad ...

  5. mysql分组排序取最大值所在行,类似hive中row_number() over partition by

    如下图, 计划实现 :按照 parent_code 分组, 取组中code最大值所在的整条记录,如红色部分.(类似hive中: row_number() over(partition by)) sel ...

  6. 在mysql中使用group by和order by取每个分组中日期最大一行数据

    转载自:https://blog.csdn.net/shiyong1949/article/details/78482737 在mysql中使用group by进行分组后取某一列的最大值,我们可以直接 ...

  7. mysql单列去重复group by分组取每组前几条记录加order by排序

    mysql分组取每组前几条记录(排名) 附group by与order by的研究,需要的朋友可以参考下 --按某一字段分组取最大(小)值所在行的数据 复制代码代码如下: /* 数据如下: name ...

  8. 取SQL分组中的某几行数据

    取SQL分组中的某几行数据 对表中数据分组,有时只需要某列的聚合值:有时却需要返回整行数据,常用的方法有:子查询.ROW_NUMBER.APPLY,总体感觉还是ROW_NUMBER比较直观.测试数据: ...

  9. SQL获取分组后取某字段最大一条记录(求每个类别中最大的值的列表)

    获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from t ...

随机推荐

  1. LAMP 系统性能调优之网络文件系统调优

    LAMP 系统性能调优之网络文件系统调优 2011-03-21 09:35 Sean A. Walberg 网络转载 字号:T | T 使用LAMP系统的用户,都想把自己LAMP性能提高运行的速度提高 ...

  2. NSMutableArray修改替换某个位置的元素

    使用NSMutableArray的replaceObjectAtIndex方法.

  3. Cassandra 安装部署

    Linux 系统安装Cassandra 一.Cassandra需要安装jdk支持,首先安装jdk 自行百度查找安装 二.下载Cassandra 官网地址: https://cassandra.apac ...

  4. linux-dns-11

    1网卡设置配置文件里面DNS服务器地址设置,2.系统默认DNS服务器地址设置.3,hosts文件指定 生效顺序是: 1 hosts文件 ---- 2 网卡配置文件DNS服务地址 ---3 /etc/r ...

  5. Codeforces 919D Substring ( 拓扑排序 && DAG上的DP )

    题意 : 给出含有 N 个点 M 条边的图(可能不连通或者包含环),每个点都标有一个小写字母编号,然后问你有没有一条路径使得路径上重复字母个数最多的次数是多少次,例如图上有条路径的顶点标号顺序是  a ...

  6. [NOIP2012] 同余方程(第三次考试大整理)

    1265. [NOIP2012] 同余方程 输入文件:mod.in   输出文件:mod.out   简单对比 时间限制:1 s   内存限制:128 MB [题目描述] 求关于 x 的同余方程 ax ...

  7. PCL智能指针疑云 <一>

    背景: 最近写了一个包,使用ndt算法拼接点云,构建三维壁面环境的点云地图. 设计一个lidar类,表征激光雷达.可以获取点云数据并存储到容器 std::vector<PointCloudPtr ...

  8. 装RAC跑脚本报错

    在执行第二个脚本的时候报错 原因是在改服务器找不到该包 解决方法: 挂载iso镜像,安装这个包

  9. java 根据省份证号-判断省份-性别-生日

    package com.nf147.manage.Test; import java.text.ParseException; import java.text.SimpleDateFormat; i ...

  10. Spring MVC 同一个方法同时返回view或json

    https://blog.csdn.net/zzg1229059735/article/details/50854778 @RequestMapping(value = "/htmlorjs ...