博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql分组(orderBy、GroupBy)获取每组前一(几)条数据
阅读量:5927 次
发布时间:2019-06-19

本文共 4179 字,大约阅读时间需要 13 分钟。

sql数据库实现分组并取每组的前1(几)条数据

测试数据准备工作:

根据某一个字段分组取最大(小)值所在行的数据:

创建表并且插入数据

CREATE table Test_orderByOrGroupBy_tb(Name nvarchar(50),Val int,Describe nvarchar(50)) insert into Test_orderByOrGroupBy_tb values('a', 1, 'a1--a的第一个值') insert into Test_orderByOrGroupBy_tb values('b', 2, 'b2b2b2b2b2b2b2b2b值') insert into Test_orderByOrGroupBy_tb values('a', 2, 'a2(a的第二个值)') insert into Test_orderByOrGroupBy_tb values('b', 1, 'b1--b的第一个值') insert into Test_orderByOrGroupBy_tb values('a', 3, 'a3:a的第三个值') insert into Test_orderByOrGroupBy_tb values('b', 3, 'b3:b的第三个值') insert into Test_orderByOrGroupBy_tb values('c', 1, 'c1c1c1c1c1c1c1c1c1c1c值')insert into Test_orderByOrGroupBy_tb values('b', 5, 'b5b5b5b5b5b5b5b5b5b5值') insert into Test_orderByOrGroupBy_tb values('c', 2, 'c2c2c2c2c2c2c2c2c2c2值') insert into Test_orderByOrGroupBy_tb values('b', 4, 'b4b4b4b4b4b4b4b4b值') GO

1、根据Name分组取Val最大的值所在行的数据。

Sql语句代码如下:

--方法1:select a.* from Test_orderByOrGroupBy_tb a where Val = (select max(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name--方法2: select a.* from Test_orderByOrGroupBy_tb a,(select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name --方法3: select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,max(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name --方法4: select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name --其中1表示获取分组中前一条数据--方法5: select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val)

上面的5种方法的sql执行执行结果一样,结果如下图:

2、根据Name分组取Val最小的值所在行的数据。

--方法1:select a.* from Test_orderByOrGroupBy_tb a where Val = (select min(Val) from Test_orderByOrGroupBy_tb where Name = a.Name) order by a.Name--方法2: select a.* from Test_orderByOrGroupBy_tb a,(select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b where a.Name = b.Name and a.Val = b.Val order by a.Name --方法3: select a.* from Test_orderByOrGroupBy_tb a inner join (select Name,min(Val) Val from Test_orderByOrGroupBy_tb group by Name) b on a.Name = b.Name and a.Val = b.Val order by a.Name --方法4: select a.* from Test_orderByOrGroupBy_tb a where 1 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val ) order by a.Name --其中1表示获取分组中前一条数据--方法5: select a.* from Test_orderByOrGroupBy_tb a where not exists(select 1 from Test_orderByOrGroupBy_tb where Name = a.Name and Val < a.Val)

上面5种方法执行结果是一样的,如下图:

3、根据Name分组取第一次出现的行所在的数据。

select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 val from Test_orderByOrGroupBy_tb where a.Name = a.Name) order by a.Name

执行结果如下图:

4、根据Name分组随机取一条数据

select a.* from Test_orderByOrGroupBy_tb a where a.Val = (select top 1 Val from Test_orderByOrGroupBy_tb where Name = a.Name order by newid()) order by a.Name

运行几次执行结果如下图:

5、根据Name分组取最大的两个(N个)Val

--方法一:select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val ) order by a.Name,a.Val --方法二:select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where Name=a.Name order by Val desc) order by a.Name,a.Val --方法三:SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and Val > a.Val having Count(*) < 2) order by a.Name

上面的三种方法执行结果是一致的如下图:

6、根据Name分组取最小的两个(N个)Val

--方法一:select a.* from Test_orderByOrGroupBy_tb a where 2 > (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val ) order by a.name,a.val --方法二:select a.* from Test_orderByOrGroupBy_tb a where val in (select top 2 val from Test_orderByOrGroupBy_tb where name=a.name order by val) order by a.name,a.val --方法三SELECT a.* from Test_orderByOrGroupBy_tb a where exists (select count(*) from Test_orderByOrGroupBy_tb where Name = a.Name and val < a.val having Count(*) < 2) order by a.name

上面的三种方法执行的结果一致如下图:

转载于:https://www.cnblogs.com/linJie1930906722/p/5983159.html

你可能感兴趣的文章
《随笔记录》20170310
查看>>
网站分析系统
查看>>
一站式解决,Android 拍照 图库的各种问题
查看>>
JavaScript匿名函数以及在循环中的匿名函数
查看>>
中国HBase技术社区第五届MeetUp ——HBase技术解析及应用实践(深圳站)
查看>>
javascript高程3 学习笔记(三)
查看>>
lsof命令
查看>>
阿里云云计算ACP考试知识点(标红为重点)
查看>>
Unhandled event loop exception PermGen space
查看>>
从零开始来看一下Java泛型的设计
查看>>
嵌入式WiFi芯片价格战已经打响 MCU企业该醒悟了
查看>>
JavaScript格式化数字显示格式
查看>>
linux视频教程之vsftp_B
查看>>
Shell编程基础
查看>>
Android获取设备已安装的应用
查看>>
理解RESTful架构
查看>>
google code for xbmc addons2
查看>>
elasticsearch2.2之javaApi
查看>>
Linux服务器数据备份
查看>>
python3 UnicodeEncodeError: 'ascii' 错误
查看>>