关键字:求连续出现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的更多相关文章

  1. 求连续数字的和------------------------------用while的算法思想

    前端代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...

  2. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

  3. 华为上机:求2的N次幂的值

    求2的N次幂的值 描述: 求2的N次幂的值(N最大不超过31,用位运算计算,结果以十六进制进行显示). 运行时间限制: 无限制 内存限制: 无限制 输入: 数字N 输出: 2的N次方(16进制,需要按 ...

  4. 写一函数,用来求表达式1+2+3+.....+n的值,并编写主函数

    Description 写一函数,用来求表达式1+2+3+.....+n的值,并编写主函数.n由键盘输入. Input 输入一个整数 Output 输出表达式的值 Sample Input 5 Sam ...

  5. 求一个Map中最大的value值,同时列出键,值

    求一个Map中最大的value值,同时列出键,值 方法1. public static void main(String[] args){  Map map=new HashMap();  map.p ...

  6. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  7. 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 ...

  8. 已知空间两点组成的直线求线上某点的Z值

    已知空间两点组成的直线求线上某点的Z值,为什么会有这种看起来比较奇怪的求值需求呢?因为真正三维空间的几何计算是比较麻烦的,很多时候需要投影到二维,再反推到三维空间上去. 复习下空间直线方程:已知空间上 ...

  9. C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等)

    获取随机数 举例:0-9 Random random = new Random(); int j = random.Next(0, 9); 0.1两个值被取值的概率相等 int a = Math.Ab ...

随机推荐

  1. 演示-JQuery中伪元素和伪类选择器

    HTML代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  2. DLL接口自动化测试总结

    1. DLL接口测试方法介绍 在最近测试的项目中,系统给业务端提供DLL文件,业务端通过DLL文件中的C++接口实现系统功能,这就需要对DLL中的C++接口进行详细功能测试. 本文主要介绍项目测试中使 ...

  3. unity3d多个版本共存

    用4.3打开两个低版本的unity工程,都报错.... 用低版本打开正常,希望Unity3D版本兼容性越来越好吧. 参考:http://blog.csdn.net/anyuanlzh/article/ ...

  4. jQuery弹出层效果

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  5. mysqlbinlog基于某个偏移量进行数据的恢复(重做),--start-position,--stop-position的使用方法

    需求描述: 今天在看mysqlbinlog的内容,看到了--start-position和--stop-position这些选项, 就测试下这个参数具体该怎么进行使用呢,在此记录下. 操作过程: 1. ...

  6. 在VS中安装/使用 MVVMLight

    一般来说,我喜欢使用NuGet来获取这些东西,比如Newtonsoft.Json.netlog4.MVVMLight 之类的东西.至于NuGet的使用,以后再说吧.为了直接进入正题,我们这里直接使用V ...

  7. 超全面的JavaWeb笔记day07<Java基础加强>

    1.myeclipse安装和使用(**) 2.debug调试模式(**) - F6: 单步执行 - F8:结束断点,后面有断点到下一个断点 3.myeclipse快捷键(**) 4.junit单元测试 ...

  8. day19<异常&File类>

    异常(异常的概述和分类) 异常(JVM默认是如何处理异常的) 异常(try...catch的方式处理异常1) 异常(try...catch的方式处理异常2) 异常(编译期异常和运行期异常的区别) 异常 ...

  9. eclipse中debug模式不能启动运行,run运行模式却能启动运行!

    这个问题我郁闷了好久!问题原因:因为断点太多了,断点冲突了. 解决办法:只要进如deug界面,选择BreakPoints选项,然后清除所有断点,再重新debug启动.问题解决! 希望能帮到遇到此问题的 ...

  10. js里面声明变量时候的注意事项

    变量名可以是中文,只能有下划线,$,数字和字母组成,开头只能以下划线(不建议使用)和字母开头.