--按某一字段分组取最大(小)值所在行的数据(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. OpenTelemetry 实战:gRPC 监控的实现原理

    前言 最近在给 opentelemetry-java-instrumentation 提交了一个 PR,是关于给 gRPC 新增四个 metrics: rpc.client.request.size: ...

  2. Flutter Engage China 开发者常见问题解答 | 下篇

    再次感谢大家对 Flutter Engage China 活动 的关注和积极参与!我们在活动前后收到了很多来自开发者的反馈和问题,Flutter 团队和演讲嘉宾在直播 Q&A 环节中也针对部分 ...

  3. Passwords

    详见 此处 Header File 0*)1190*+0**0).0970)/0)/111105000

  4. const` 关键字位于函数签名的末尾

    在 C++ 中,const 关键字可以应用于成员函数,表示该函数不会修改对象的成员变量. const 出现在 operator->() 成员函数的末尾,这意味着该成员函数在调用时不会修改对象的任 ...

  5. Kubernetes 持久化存储之 NFS 终极实战指南

    作者:运维有术星主 在 Kubernetes 生态系统中,持久化存储扮演着至关重要的角色,它是支撑应用稳定运行的基石.对于那些选择自建 Kubernetes 集群的运维架构师而言,选择合适的后端持久化 ...

  6. 基于 WeDataSphere Prophecis 与 KubeSphere 构建云原生机器学习平台

    KubeSphere 开源社区的小伙伴们,大家好.我是微众银行大数据平台的工程师周可,接下来给大家分享的是基于 WeDataSphere 和 KubeSphere 这两个开源社区的产品去构建一个云原生 ...

  7. KubeSphere 3.1.1 发布,可以接入集群已有的 Prometheus

    KubeSphere 作为一款面向应用的开源容器平台,经过 3 年的发展和 10 个版本的迭代,收获了两百多位开源贡献者,超过十万次下载,并有数千名社区用户用 KubeSphere 作为企业容器平台. ...

  8. 在华为云上安装高可用 KubeSphere

    随着多云多集群的场景越来越丰富,在各个云厂商环境部署 KubeSphere 的需求随之升高.由于各云厂商的云资源使用规则和菜单导航栏各不相同,会使用户花大量时间去排错.为降低部署过程错误率,本教程使用 ...

  9. 华为云-云容器引擎(CCE)-高危操作及解决方案

    业务部署或运行过程中,用户可能会触发不同层面的高危操作,导致不同程度上的业务故障.为了能够更好地帮助用户预估及避免操作风险,本文将从集群/节点维度出发,为用户展示哪些高危操作会导致怎样的后果,以及为用 ...

  10. Visual Studio登录页面打不开无法登录的解决

    我也是折腾了好久-- 1. 打开Visual Studio-->工具-->选项-->账户,找到了登录配置 2. 将嵌入式Web浏览器改为"Windows身份认证中转站&qu ...