求連續出現5次以上的值,並且取第5次所在id

2022-02-15 04:42:45 字數 4406 閱讀 6599

關鍵字:求連續出現5次以上的值,並且取第5次所在id

關鍵字:求在某列連續出現n次值的的資料,並且取第m次出現所在行

需求,求連續出現5次以上的值,並且取第5次所在id

測試資料

create

table

temp1 (

id intprimary

keyidentity(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 ) )

asy,

(row_number()

over(partition by x order

byid)

) asz

from

t1 a

) bwhere y>=

5and 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 )

asy,

(select

count(1) from t1 where x=a.x and id <=

a.id

) asz

from

t1 a

) bwhere y>=

5and z=

5mysql

(1)臨時表方法

create

table

test1 (

id intprimary

keyauto_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

) twhere y>=

5and 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

) twhere 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

) twhere y>=5 and z=5

原表資料:

結果:

連續出現k次的字元

描述 給定乙個字串,在字串中找到第乙個連續出現至少k次的字元。輸入第一行包含乙個正整數k,表示至少需要連續出現的次數。1 k 1000。第二行包含需要查詢的字串。字串長度在1到1000之間,且不包含任何空白符。輸出若存在連續出現至少k次的字元,輸出該字元 否則輸出no。樣例輸入 3 abcccaaa...

求連續子串行的最大值

問題描述 有一串數字 可正可負的int,放在陣列num裡 要求找到起始位置start和終止位置end,使得從start位置到end位置的所有數字之和最大,返回這個最大值max。演算法思想 使用動態規劃。設 f x 為以 a x 終止且包含 a x 的最大序列的和,有 f 1 a 1 f x 1 f ...

去掉字串連續出現的k次

移除字串中連續出現的k個0 可以用掃瞄字元陣列的解法,但是用正規表示式更為快捷 package 4字串 public class k去掉字串連續出現的k次 private static string zehengze string a,int k return a.replaceall regexp...