文章参考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. Maven:mirror和repository

    1 Repository(仓库) 1.1 Maven仓库主要有2种: remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问 local repository ...

  2. 【leetcode】Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  3. 【leetcode】Path Sum IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  4. 对Node.js 中的依赖管理的研究-----------------引用

    nodejs依赖:  dependencies   devDependencies   peerDependencies  bundledDependencies  optionalDependenc ...

  5. 织梦dedecms自定义表单导出到excel教程

    不写死任何字段,不写死任何东西,修改2个文件,让织梦自定义表单自由导出到Excel表格里. 添加教程 1.\dede\templets\diy_main.htm 找到 前台预览</a> 在 ...

  6. 电脑右键新建没有xmind文件选项解决方法

    xmind还是方便的. 打开注册表,展开HKEY_CLASSES_ROOT,展开.xmind(如果没有请新建).在里面新建ShellNew项,并展开,在里面新建NullFile这个字符串值. 如果还是 ...

  7. js 反斜杠 处理

    var t = jsonstr.replace(/\\/g,"\\\\\\\\"); --\\ 表示 代码 输出 \' 单引号 \" 双引号 \& 和号 \\ 反 ...

  8. Devexpress 10

    序言 Grid表格 资料 https://www.devexpresscn.com/

  9. luogu 4725 【模板】多项式对数函数(多项式 ln)

    $G(x)=ln(A(x))$ $G'(x)=ln'(A(x))A'(x)=\frac{A'(x)}{A(x)}$     由于求导和积分是互逆的,所以对 $G$ 求积分,即 $G(x)=\int\f ...

  10. BZOJ 4597: [Shoi2016]随机序列 线段树 + 思维

    Description 你的面前有N个数排成一行.分别为A1, A2, … , An.你打算在每相邻的两个 Ai和 Ai+1 间都插入一个加号或者 减号或者乘号.那么一共有 3^(n-1) 种可能的表 ...