--按某一字段分组取最大(小)值所在行的数据(2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a 2 a2(a的第二个值)
a 1 a1--a的第一个值
a 3 a3:a的第三个值
b 1 b1--b的第一个值
b 3 b3:b的第三个值
b 2 b2b2b2b2
b 4 b4b4
b 5 b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a', 2, 'a2(a的第二个值)')
insert into tb values('a', 1, 'a1--a的第一个值')
insert into tb values('a', 3, 'a3:a的第三个值')
insert into tb values('b', 1, 'b1--b的第一个值')
insert into tb values('b', 3, 'b3:b的第三个值')
insert into tb values('b', 2, 'b2b2b2b2')
insert into tb values('b', 4, 'b4b4')
insert into tb values('b', 5, 'b5b5b5b5b5')
go --一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
/*
name val memo
---------- ----------- --------------------
a 3 a3:a的第三个值
b 5 b5b5b5b5b5
*/ --二、按name分组取val最小的值所在行的数据。
--方法1:
select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
--方法3:
select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 1 b1--b的第一个值
*/ --三、按name分组取第一次出现的行所在的数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
*/ --四、按name分组随机取一条数据。
select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
b 5 b5b5b5b5b5
*/ --五、按name分组取最小的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 1 a1--a的第一个值
a 2 a2(a的第二个值)
b 1 b1--b的第一个值
b 2 b2b2b2b2
*/ --六、按name分组取最大的两个(N个)val
select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name
/*
name val memo
---------- ----------- --------------------
a 2 a2(a的第二个值)
a 3 a3:a的第三个值
b 4 b4b4
b 5 b5b5b5b5b5
*/
create table tb(id1 int,id2 int,a int,b int ,c int)
insert into tb values(1, 1, 5, 4, 3)
insert into tb values(2, 1, 8, 6, 4)
insert into tb values(1, 2, 6, 7, 2)
insert into tb values(2, 2, 10, 5, 4)
insert into tb values(1, 3, 10, 1, 1)
insert into tb values(2, 3, 12, 2, 2)
go --方法1:
select a.* from tb a where a = (select max(a) from tb where id2 = a.id2) order by a.id2
--方法2:
select a.* from tb a where not exists(select 1 from tb where id2 = a.id2 and a > a.a) order by a.id2
--方法3:
select a.* from tb a,(select id2,max(a) a from tb group by id2) b where a.id2 = b.id2 and a.a = b.a order by a.id2
--方法4:
select a.* from tb a inner join (select id2 , max(a) a from tb group by id2) b on a.id2 = b.id2 and a.a = b.a order by a.id2
--方法5
select a.* from tb a where 1 > (select count(*) from tb where id2 = a.id2 and a > a.a ) order by a.id2 drop table tb /*
id1 id2 a b c
----------- ----------- ----------- ----------- -----------
2 1 8 6 4
2 2 10 5 4
2 3 12 2 2 (所影响的行数为 3 行) id1 id2 a b c
----------- ----------- ----------- ----------- -----------
2 1 8 6 4
2 2 10 5 4
2 3 12 2 2 (所影响的行数为 3 行) id1 id2 a b c
----------- ----------- ----------- ----------- -----------
2 1 8 6 4
2 2 10 5 4
2 3 12 2 2 (所影响的行数为 3 行) id1 id2 a b c
----------- ----------- ----------- ----------- -----------
2 1 8 6 4
2 2 10 5 4
2 3 12 2 2 (所影响的行数为 3 行) id1 id2 a b c
----------- ----------- ----------- ----------- -----------
2 1 8 6 4
2 2 10 5 4
2 3 12 2 2 (所影响的行数为 3 行)
*/

SQL排序分组的更多相关文章

  1. SQL语句分组排序,多表关联排序

    SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...

  2. Hadoop日记Day18---MapReduce排序分组

    本节所用到的数据下载地址为:http://pan.baidu.com/s/1bnfELmZ MapReduce的排序分组任务与要求 我们知道排序分组是MapReduce中Mapper端的第四步,其中分 ...

  3. SQL Server 分组后取Top N

    SQL Server 分组后取Top N(转) 近日,工作中突遇一需求:将一数据表分组,而后取出每组内按一定规则排列的前N条数据.乍想来,这本是寻常查询,无甚难处.可提笔写来,终究是困住了笔者好一会儿 ...

  4. 第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all; Select 列 into 新表;字符串函数;日期函数

    第17课-数据库开发及ado.net 聚合函数,模糊查询like,通配符.空值处理.order by排序.分组group by-having.类型转换-cast,Convert.union all;  ...

  5. CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏

    CASE函数   作用: 可以将查询结果集的某一列的字段值进行替换 它可以生成一个新列 相当于switch...case和 if..else 使用语法: case 表达式/字段 when 值 then ...

  6. 【mysql】关联查询_子查询_排序分组优化

    1. 关联查询优化 1.1 left join 结论: ①在优化关联查询时,只有在被驱动表上建立索引才有效! ②left join 时,左侧的为驱动表,右侧为被驱动表! 1.2 inner join ...

  7. PCB MS SQL 排序应用---SQL相邻数据区间值求解

    其中一篇 博文中有写<PCB MS SQL 排序应用---相邻数据且相同合并处理>此篇有也应相用也同的技巧,实现相邻数据区间值求解 示例: 原数据:处理前 求出区间值:处理后 SQL 代码 ...

  8. sql 连续分组判断 partition by

    partition by 会根据分类字段进行排序 加上rownum 可以形成 每组从1开始重新排序 举个例子, 我要根据时间为依据,连续出现合并为一组,统计每组在区间里的次数 ------------ ...

  9. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  10. SQL之分组排序取top n

    转自:http://blog.csdn.net/wguangliang/article/details/50167283 要求:按照课程分组,查找每个课程最高的两个成绩. 数据文件如下: 第一列no为 ...

随机推荐

  1. Comfyui 基础教程(一) —— 本地安装部署

    前言 前面一篇文章已经介绍过,ComfyUI 和 Stable Diffusion 的关系.不清楚的朋友,看传送门 Stable Diffusion 小白的入坑铺垫 . WebUI 以及 ComfyU ...

  2. 为什么在 C++ 中,类的静态成员变量需要在源文件中进行定义?

    为什么在 C++ 中,类的静态成员变量需要在源文件中进行定义? 类的静态成员变量需要在源文件中进行定义,以便在链接阶段能够正确地分配内存并为其分配地址. 当你在类的头文件中声明一个静态成员变量时,这只 ...

  3. CSS单位em、rem、vh和vw等及CSS3的calc()以及line-height百分比

    css单位我们常用的是px,也即是像素.随着网页开发自适应的要求,css3新增了许多单位,rem.vw和vh.vmin和vmax.ch和ex等. em 做前端的应该对em不陌生,不是什么罕见的单位,是 ...

  4. 算法学习-CDQ分治

    对于二维偏序,为普通的求逆序对,只需要先排序一遍,然后树状数组或双指针即可 而三位偏序甚至更高,则需要用 CDQ 分治,简单来说,就是将树状数组和双指针结合 操作步骤如下: 1.开始将数组按第一维排序 ...

  5. Parquet.Net: 将 Apache Parquet 移植到 .NET

    Parquet.Net 是一个用于读取和写入 Apache Parquet 文件的纯 .NET 库,使用MIT协议开源,github仓库:https://github.com/aloneguid/pa ...

  6. QT疑难杂症之QML程序中如何使用文件系统模型QFileSystemModel?

    简介 本文介绍了 QML程序中如何使用树状控件TreeView展示QT文件系统模型QFileSystemModel中的数据,并给出了两种实现模式. 目录 QML程序中使用文件系统模型的代码 树状控件自 ...

  7. 命令行gcc -v和g++ -v输出版本不一致

    命令行gcc -v和g++ -v输出版本不一致 前言:本文初编辑于2024年1月30日 CSDN主页:https://blog.csdn.net/rvdgdsva 博客园主页:https://www. ...

  8. USB3.0与USB2.0编码方式的区别

    首先,USB3.0传输的编码方式和USB2.0本质上是不同的. 1.USB3.0的编码方式 USB 3.0采用的是8b/10b编码方式,由于高速传输,信号干扰的问题,USB 3.0采用 8/10bit ...

  9. Trace32 simulator调试以及简单实用命令介绍

    目录 Trace32 Simulator debug Trace32工具配置 Trace32命令简介 memory class 常见命令索引 v.v使用实例 不同CPU运行信息查看 Trace32 S ...

  10. pstore

    简介 pstore文件系统(是的,这是个文件系统)是Persistent Storage的缩写,最早在2010年由 Tony Luck 设计并合入Linux主分支,设计的初衷是在内核Panic/Oop ...