SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州) /* 数据如下:name val memoa 2 a2(a的第二个值)a 1 a1--a的第一个值a 3 a3:a的第三个值b 1 b1--b的第一个值b 3 b3:b的第三个值b 2 b2b2b2b2b 4 b4b4b 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.valselect 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.valselect 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*/SQL Server 按某一字段分组 取 最大 (小)值所在行的数据的更多相关文章
- mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据 mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结 ...
- SQL Server数据库自增字段正确的插入值的描述
我们今天主要向大家讲述的是SQL Server数据库之向SQL Server自增字段正确的插入值的实际操作步骤,在一般的情况下,我们不能向 SQL Server 数据库自增字段中插入值,如果非要这么干 ...
- SQL Server数据库————连接查询和分组查询
SQL Server数据库————连接查询和分组查询 分组查询 select 列from <表名> where …… group by 列 注意:跟order by一样group ...
- SQL Server表描述 及 字段描述的增、删、改、查询
SQL Server表描述 及 字段描述的增.删.改.查询 --测试: --创建表及描述信息 ),isname )) --为表添加描述信息 EXECUTE sp_addextendedproperty ...
- SQL SERVER 表添加新字段
SQL SERVER 表添加新字段 ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL; -- doc_exa 是表名 -- column_b 是新加的 ...
- SQL Server的镜像是基于物理块变化的复制 镜像Failover之后数据的预热问题
SQL Server的镜像是基于物理块变化的复制 镜像Failover之后数据的预热问题 基于物理块变化的复制,没有并行也是很快的. 逻辑复制的日志是按事务结束的时间排序的,而物理复制是与事务无关的, ...
- SQL SERVER技术内幕之8 分组集
分组集就是分组(GROUP BY子句)使用的一组属性,在传统的SQL中,一个聚合查询只能定义一个分组集: 假设现在不想生成4个单独的结果集,而是希望生成一个统一的结果集,其中包含所有4个分组集的聚合 ...
- oracle 根据字段分组取第一条数据及rank函数说明
当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...
- [转]Sql Server 给表与字段添加描述
/* 在SQL语句中通过系统存储过sp_addextendedproperty可为表字段添加上动态的说明(备注)下面是SQL SERVER帮助文档中对sp_addextendedproperty存储过 ...
随机推荐
- 【BZOJ 2243】染色 - 树链剖分+线段树
#include <cstdio> #include <cstring> #include <cstdlib> using namespace std; const ...
- String的那一大堆事儿--1
perfTimeStr = perfTimeStr.replace(perfTimeStr.substring(0,4), "____"); perfTimeStr = perfT ...
- 有关Rander生成随机数的问题
首先我们说的是要生成一个随机数要求传入两个参数.一个表示生成的个数,另外一个表示生成的长度 . public void shengchengsuijishu(int lenght) { '}; Ran ...
- (28)odoo中css可用颜色对照表
颜色 颜色英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 ...
- Linux GDB常用命令一栏
Linux GDB 常用命令如下: 1.启动和退出gdb (1)启动:gdb ***:显示一段版权说明: (*** 表示可执行程序名) (2)退出:quit.有的时候输入quit后会出现相关提示:类似 ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 198. 213. 337. House Robber -- 不取相邻值的最大值
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- (转载)Android理解:显式和隐式Intent
Intent分两种:显式(Explicit intent)和隐式(Implicit intent). 一.显式(设置Component) 显式,即直接指定需要打开的activity对应的类. 以下多种 ...
- Farseer.Net
Farseer.Net V0.2 ORM开源框架 目录 http://www.cnblogs.com/steden/archive/2013/01/22/2871160.html V1.0教程:htt ...
- 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...