--按某一字段分组取最大(小)值所在行的数据(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. 【Python】Selenium自动化测试之滑块拼图验证码图片方法

    在项目中有时验证码是滑块拼图形式的,这种验证码该如何完成验证呢? 有以下几个步骤: 目录 第一步:得到验证码图片 第二步:匹配缺口照片在完整照片的位置 第三步:机器模拟人工滑动轨迹 第四步:判断拼图是 ...

  2. Drools决策表实践运用

    Drools 决策表的使用与说明 Drools决策表的使用 官方文档决策表说明 决策表使用方式 执行drl代码及结果 Drools决策表的使用 官方文档决策表说明 Drools 决策表的使用 16.7 ...

  3. CMake构建学习笔记14-依赖库管理工具

    如果说做C/C++开发最大的痛点是什么,那么一定是缺少一个官方的统一的包管理器.认真的说,如果你要用C/C++干点什么,至少需要(Windows系统下): C/C++语言本身.标准库.以及操作系统AP ...

  4. Java怎么把多个对象的list的数据合并

    1.示例一:创建几个包含Person对象的List,并将它们合并成一个新的List 在Java中,将多个对象的List合并通常涉及到遍历这些List并将它们的元素添加到一个新的List中.这里,我将给 ...

  5. ASP.NET Core – Razor Class Library (RCL)

    前言 Razor Class Library 的用途是封装 Razor views, pages, controllers, page models, Razor components, View c ...

  6. 【Abyss】Android 平台应用级系统调用拦截框架

    Android平台从上到下,无需ROOT/解锁/刷机,应用级拦截框架的最后一环 -- SVC系统调用拦截. ☞ Github ☜ 由于我们虚拟化产品的需求,需要支持在普通的Android手机运行.我们 ...

  7. servlet一些笔记、详解

    一.什么是servlet? 处理请求和发送响应的过程是由一种叫做Servlet的程序来完成的,并且Servlet是为了解决实现动态页面而衍生的东西.理解这个的前提是了解一些http协议的东西,并且知道 ...

  8. SpringMVC —— 请求参数

    请求映射路径    请求方式   get请求传参        post请求传参          POST请求中文参数乱码问题        请求参数(五种类型数据参数)               ...

  9. AtCoder Regular Contest 182(A B C)

    原来第二题比第一题简单吗 A.Chmax Rush! \(\texttt{Diff 1110}\) 给定三个序列 \(S,P,V\),其中 \(S\) 的长度为 \(N\),\(P,V\) 的长度为 ...

  10. Kulla-Conty BRDF补充_重要性采样GGX

    重要性采样的方法有多种,这里我们介绍其中一种,你的实现也可以使用其他方法.我们将通过 GGX 采样来完成 E(µ)的预计算工作.先从理论上讨论 GGX 采样算法,对于给定出射方向 o 的 GGX采样, ...