关键字:求连续出现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. Ubuntu 13.04 安装 Oracle11gR2

    #step 1: groupadd -g 2000 dbauseradd -g 2000 -m -s /bin/bash -u 2000 griduseradd -g 2000 -m -s /bin/ ...

  2. php的优缺点

    1. 跨平台,性能优越,跟Linux/Unix结合别跟Windows结合性能强45%,并且和很多免费的平台结合非常省钱,比如LAMP(Linux /Apache/Mysql/PHP)或者FAMP(Fr ...

  3. strip() 、lstrip() 、rstrip()

    strip() 用于移除字符串开头和结尾的空格或换行符,如果指定参数,则表示移除指定的字符lstrip() 用于移除字符串开头的空格或换行符,如果指定参数,则表示移除指定的字符rstrip() 用于移 ...

  4. hasattr() 、getattr() 、setattr()

    hasattr(object, name) :用于判断一个对象中是否有指定的属性或方法,如果存在返回 True,否则返回 False getattr(object, name, [default]) ...

  5. Receiver type for instance message is a forward

    本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&catalog=0&sort=time&p=3 这往往是引用的问题.ARC要 ...

  6. java高级---->Thread之CountDownLatch的使用

    CountDownLatch是JDK 5+里面闭锁的一个实现,允许一个或者多个线程等待某个事件的发生.今天我们通过一些实例来学习一下它的用法. CountDownLatch的简单使用 CountDow ...

  7. web基础----->servlet中得到请求的数据

    对tomcat的源码做一些分析,今天我们就开始servlet中的请求分析. form表单中的默认类型 一.在index.jsp中get请求: <form action="Paramet ...

  8. mysql导入数据失败:mysql max_allowed_packet 设置过小

    mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会受max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...

  9. npm安装出错的时候,如何使用国内的镜像!--解决办法

    在前端开发领域,Node已经很普遍了,使用Node就会使用到一些包.所以常用的 npm 就会经常使用得到,但是在使用 npm 安装一些包的过程中,会发现安装的速度会很慢,而且很多时候直接安装不了. 百 ...

  10. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...