求连续出现5次以上的值,并且取第5次所在id
关键字:求连续出现5次以上的值,并且取第5次所在id
关键字:求在某列连续出现N次值的的数据,并且取第M次出现所在行
需求,求连续出现5次以上的值,并且取第5次所在id

SQL SERVER:
--测试数据
CREATE TABLE temp1 (
id INT PRIMARY KEY identity(1,1),
num1 INT,
num2 INT
);
insert into temp1 values( 11,51),( 12,52);
insert into temp1 values( 10,101),( 10,102),( 10,103),( 10,104),( 10,105),( 10,106),( 10,107);
insert into temp1 values( 13,53),( 14,54);
insert into temp1 values( 10,108),( 10,109),( 10,110);
insert into temp1 values( 15,55),( 16,56);
insert into temp1 values( 10,111),( 10,112),( 10,113),( 10,114),( 10,115),( 10,116),( 10,117); --解决代码
(1)
;with t1 as (
select *,id-row_number() over(partition by num1 order by id) x from temp1
)
select * from
(
select *,
(
count(1) over(partition by x )
) as y,
(
row_number() over(partition by x order by id)
) as z
from t1 a
) b
where y>=5 and z=5 (2)
;with t1 as (
select *,id - row_number() over(partition by num1 order by id) x from temp1
)
select * from
(
select *,
(
select count(1) from t1 where x=a.x
) as y,
(
select count(1) from t1 where x=a.x AND id <=a.id
) as z
from t1 a
) b
where y>=5 and z=5
mysql
(1)临时表方法
CREATE TABLE test1 (
id INT PRIMARY KEY auto_increment,
num1 INT,
num2 INT
);
insert into test1 values(null,11,51),(null,12,52);
insert into test1 values(null,10,101),(null,10,102),(null,10,103),(null,10,104),(null,10,105),(null,10,106),(null,10,107); insert into test1 values(null,13,53),(null,14,54);
insert into test1 values(null,10,108),(null,10,109),(null,10,110);
insert into test1 values(null,15,55),(null,16,56);
insert into test1 values(null,10,111),(null,10,112),(null,10,113),(null,10,114),(null,10,115),(null,10,116),(null,10,117); CREATE TABLE test2 like test1;
alter table test2 change id id int;
alter table test2 add rn int unique auto_increment;
insert into test2(id,num1,num2) select * from test1 where num1=10; select *,id-rn as x from test2; select * from (
select *,
(select count(1) from (select *,id-rn as x from test2) t where t.x=t1.x) y,
(select count(1) from (select *,id-rn as x from test2) t where t.x=t1.x and t.id <= t1.id) z
from (select *,id-rn as x from test2) t1
) t
where y>=5 and z=5 (2)构造row_number()方法
select * from (
select *,
(select count(1) from (select *,id-rn as x from (select test1.*, @num:=@num+1 as rn from test1 join (select @num:=0) temp1 where test1.num1=10) temp2) t where t.x=t1.x) y,
(select count(1) from (select *,id-rn as x from (select test1.*,@num1:=@num1+1 as rn from test1 join (select @num1:=0) temp1 where test1.num1=10) temp2 ) t where t.x=t1.x and t.id <= t1.id) z
from (select *,id-rn as x from (select test1.*,@num2:=@num2+1 as rn from test1 join (select @num2:=0) temp1 where test1.num1=10) temp2) t1
) t
where y>=5 and z=5
--再简化
select * from (
select *,
(select count(1) from (select test1.*, id-@num:=@num+1 as x from test1 join (select @num:=0) temp1 where test1.num1=10) t where t.x=t1.x) y,
(select count(1) from (select test1.*,id-@num1:=@num1+1 as x from test1 join (select @num1:=0) temp1 where test1.num1=10) t where t.x=t1.x and t.id <= t1.id) z
from (select test1.*,id-@num2:=@num2+1 as x from test1 join (select @num2:=0) temp1 where test1.num1=10) t1
) t
where y>=5 and z=5
原表数据:

结果:

求连续出现5次以上的值,并且取第5次所在id的更多相关文章
- 求连续数字的和------------------------------用while的算法思想
前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
- 华为上机:求2的N次幂的值
求2的N次幂的值 描述: 求2的N次幂的值(N最大不超过31,用位运算计算,结果以十六进制进行显示). 运行时间限制: 无限制 内存限制: 无限制 输入: 数字N 输出: 2的N次方(16进制,需要按 ...
- 写一函数,用来求表达式1+2+3+.....+n的值,并编写主函数
Description 写一函数,用来求表达式1+2+3+.....+n的值,并编写主函数.n由键盘输入. Input 输入一个整数 Output 输出表达式的值 Sample Input 5 Sam ...
- 求一个Map中最大的value值,同时列出键,值
求一个Map中最大的value值,同时列出键,值 方法1. public static void main(String[] args){ Map map=new HashMap(); map.p ...
- 求集合中选一个数与当前值进行位运算的max
求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...
- leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)
题目描述: In a given integer array nums, there is always exactly one largest element. Find whether the l ...
- 已知空间两点组成的直线求线上某点的Z值
已知空间两点组成的直线求线上某点的Z值,为什么会有这种看起来比较奇怪的求值需求呢?因为真正三维空间的几何计算是比较麻烦的,很多时候需要投影到二维,再反推到三维空间上去. 复习下空间直线方程:已知空间上 ...
- C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)
获取随机数 举例:0-9 Random random = new Random(); int j = random.Next(0, 9); 0.1两个值被取值的概率相等 int a = Math.Ab ...
随机推荐
- asp.net session的使用与过期实例代码
Session的使用 <head runat="server"> <title></title> <script src=&q ...
- OpenCV学习:Windows+VS2010+OpenCV配置
OpenCV下载: 百度云下载:https://pan.baidu.com/s/1mhAExdu (2.4.9版本) 下载完成后,双击运行exe,选择输出目录,我选择的是C:\OpenCV 配置环境变 ...
- web开发中的安全问题
web开发中很多东西由前段来负责判断,比如常见的邮箱 电话号码,前端判断到不是一个正确的格式,在你点击提交时候提示你格式填错了,然后不请求后端php,直到你填写正确的格式为止.这种其实可以修改js或者 ...
- jquery 中的回调函数,回调函数(callback)是什么?
知乎上果然大牛比较多 大神解释如下: 你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里, ...
- Windows之Xmanager连接linux打开Oracle视图操作
前提:安装Xmanager 能够百度Xmanager下载其破解版或者带注冊机的版本号,也能够官网下载.只是须要秘钥(建议下载企业版) 官网下载地址:http://www.netsarang.com/d ...
- 2014年辛星解读Javascript之DOM之事件及其绑定
我们通过DOM的事件能够对HTML的事件作出反应.就像我们用其它编程语言写GUI一样,那么HTML包含哪些事件呢?以下是几个常见的样例,比方网页已经完毕记载,图像完毕载入,鼠标移动到元素上方.输入文字 ...
- PHP-001
ThinkPHP单字母函数(快捷方法)使用总结 http://www.cnblogs.com/caicaizi/p/5173093.html
- python2.0_s12_day15_django框架的基本使用
day15本节内容介绍 上节作业讲解(让行进入编辑模式,批量编辑) CSS之特殊内容补充 CSS内容补充之伪类 伪类实例:返回顶部终极版 CSS内容补充之无法被覆盖 jQuery插件 jQuery插件 ...
- scala函数进阶篇
1.求值策略scala里有两种求值策略Call By Value -先对函数实参求值,在函数体中用这个求出的参数值.Call By Name -先不对函数实参求值,而是函数实参每次在函数体内被用到时都 ...
- Python 入门(五)条件判断和循环
if语句 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = 20 if age > ...