当前位置: 首页 > news >正文

sql分组(orderBy、GroupBy)获取每组前一(几)条数据

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

相关文章:

  • mysql实现vsftp虚拟用户访问
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • makefile 一看就懂了
  • 统计服务连接状况
  • 什么是wall-clock time
  • Quartz 框架的应用
  • 直接启动tomcat时为tomcat指定JDK 而不是读取环境变量中的配置
  • php 路径
  • 服务器之间,相同帐号,实现免密钥登录
  • 【noi 2.6_9289】Ant Counting 数蚂蚁{Usaco2005 Nov}(DP)
  • 数据获取以及处理系统 --- 功能规格说明书
  • 【JAVA】设计模式之懒汉式与恶汉式的单例模式实现的方法与详解
  • asp.net定时任务
  • 14. Html5的局:WebGL的纹理格式
  • Tomcat编译jsp生成Servlet文件的存放位置
  • ES6指北【2】—— 箭头函数
  • python3.6+scrapy+mysql 爬虫实战
  • [case10]使用RSQL实现端到端的动态查询
  • 2017-08-04 前端日报
  • 3.7、@ResponseBody 和 @RestController
  • Apache的基本使用
  • Date型的使用
  • JavaScript设计模式与开发实践系列之策略模式
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • KMP算法及优化
  • Linux快速复制或删除大量小文件
  • Lsb图片隐写
  • MySQL的数据类型
  • node.js
  • Vue UI框架库开发介绍
  • vuex 笔记整理
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 区块链技术特点之去中心化特性
  • 如何合理的规划jvm性能调优
  • 入职第二天:使用koa搭建node server是种怎样的体验
  • 《码出高效》学习笔记与书中错误记录
  • 2017年360最后一道编程题
  • gunicorn工作原理
  • 仓管云——企业云erp功能有哪些?
  • ​【已解决】npm install​卡主不动的情况
  • ​2020 年大前端技术趋势解读
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • #162 (Div. 2)
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • $NOIp2018$劝退记
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (Java数据结构)ArrayList
  • (第二周)效能测试
  • (分类)KNN算法- 参数调优
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (接口自动化)Python3操作MySQL数据库
  • (五)MySQL的备份及恢复
  • (原創) 如何解决make kernel时『clock skew detected』的warning? (OS) (Linux)
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)