如下表Z 中,取 字段a 最大的那行

字段a           字段a    字段c
SP000016964 5 20
SP000016964 7 30
SP000016964 1 15
SP000017755 4 16
SP000017755 8 12

最后得到下面结果

SP000016964 7 30
SP000017755 8 12

代码如下 复制代码
SQL语句如下:select z.* from z, (select a,max(b) as max_b from z group by a) z1 where z.a = z1.a and z.b = z1.max_b

下面给大家详细总结一些方法

SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
-- 按某一字段分组 取 最大 (小)值所在行的数据
-- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 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的第一个值www.111Cn.net ' )
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分组 随机取 一条数据www.111cn.net
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
*/

更多详细内容请查看:http://www.111cn.net/database/mssqlserver/57825.htm

SQL取某个字段最大(小)数值及其相应行的其他字段值的句语的更多相关文章

  1. sql取逗号前后数据与批量修改某一字段某一值

    sql取逗号后的值 SELECT SUBSTRING_INDEX(字段,) FROM 表名 sql取逗号前的值 SELECT SUBSTRING_INDEX(字段,) FROM 表名 批量修改 UPD ...

  2. Oracle Sales Cloud:管理沙盒(定制化)小细节2——使用对象触发器更新数字字段

    在上一篇 "管理沙盒(定制化)小细节1" 的随笔中,我们使用公式法在 "业务机会" 对象(单头)上建立了 "利润合计" 字段,并将它等于 & ...

  3. 小书MybatisPlus第9篇-常用字段默认值自动填充

    本文为Mybatis Plus系列文章的第9篇,前8篇访问地址如下: 小书MybatisPlus第1篇-整合SpringBoot快速开始增删改查 小书MybatisPlus第2篇-条件构造器的应用及总 ...

  4. sql取整函数

    SQL取整运算2009年04一个月02日本 星期四 10:01有使用说明这种方法记录,就在今天,那么当仍然被遗忘.事实上通常用四舍五入的操作有几种情况,一个是简单的四舍五入,无论是小数点后面的是什么都 ...

  5. MySQL基本语法(一):和SQL Server语法的差异小归纳

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  6. 【HANA系列】SAP HANA SQL取表中每行最小值

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL取表中每 ...

  7. 聊聊sql优化的15个小技巧

    前言 sql优化是一个大家都比较关注的热门话题,无论你在面试,还是工作中,都很有可能会遇到. 如果某天你负责的某个线上接口,出现了性能问题,需要做优化.那么你首先想到的很有可能是优化sql语句,因为它 ...

  8. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  9. 每日学习心得:SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)

    2013-8-20 1.    SQL查询表的行列转换/小计/统计(with  rollup,with cube,pivot解析) 在实际的项目开发中有很多项目都会有报表模块,今天就通过一个小的SQL ...

随机推荐

  1. Codeforces Round #342 (Div. 2) D. Finals in arithmetic 贪心

    D. Finals in arithmetic 题目连接: http://www.codeforces.com/contest/625/problem/D Description Vitya is s ...

  2. C#生成软件注册码

    开发软件时,当用到商业用途时,注册码与激活码就显得很重要了.现在的软件破解技术实在在强了,各种国内外大型软件都有注册机制,但同时也不断地被破解.下面发的只是一个常用版本,发出源码被破就更容易了,但我们 ...

  3. [Express] Level 5: Route Instance -- refactor the code

    Route Instance Let's rewrite our cities routes using a Route Instance. Create a new Route Instance f ...

  4. [Express] Level 4: Body-parser -- Post

    Parser Setup Assume the body-parser middleware is installed. Now, let's use it in our Express applic ...

  5. Lvalue, Rvalue, Xvalue, Prvalue, Glvalue

    c++11中关于什么是lvalue, 什么是rvalue, 什么是xvalue, 什么是prvalue, 什么是… 一直搞得我晕头转向的, 今天下定决心一定要把它搞定, 写了一个程序来判断lvalue ...

  6. 解决ajax.net 1.0中文乱码问题!

      在使用ajax.net的UpdatePanel的时候,当requestEncoding编码为GB2312的时候,出现乱码.如果要解决这个问题最简单的就是改用utf-8了,但是原来使用GB2312, ...

  7. QtInternal 之 高效使用QString

    注意:本文翻译自  http://developer.qt.nokia.com   中的  UsingQStringEffectively   ,中文译文见  简体中文版 ,如果你对翻译wiki感兴趣 ...

  8. UBIFS FAQ and HOWTO

    转:http://www.linux-mtd.infradead.org/faq/ubifs.html UBIFS FAQ and HOWTO Table of contents How do I e ...

  9. LOG4NET开源日志dll引用流程,在net3.5中已经实践ok

    一,在app.config中配置 <?xml version="1.0"?><configuration> <configSections> & ...

  10. 1.4.2 solr字段类型--(1.4.2.5)使用枚举字段

    1.4.2 solr字段类型 (1.4.2.1) 字段类型定义和字段类型属性. (1.4.2.2) solr附带的字段类型 (1.4.2.3) 使用货币和汇率 (1.4.2.4) 使用Dates(日期 ...