mysql 最大连续出现次数_mysql或oracle查询连续相等最大次数

-- Table structure for temp_param_result

DROP TABLE IF EXISTS temp_param_result; CREATE TABLE temp_param_result ( id int(11) NOT NULL, paramCode varchar(255) DEFAULT NULL, resultStr varchar(255) DEFAULT NULL, createdt datetime DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-----------------------------------------------mysql 解决方案------------------------------------------- SELECT aa.paramCode, aa.resultStr, max(rownum) maxrow FROM ( SELECT ib.paramCode, ib.resultStr, @pname, @paramCode, IF ( (ib.resultStr = @pname && ib.paramCode = @paramCode), @ROW :=@ROW + 1, @ROW := 1 ) rownum ,@pname := ib.resultStr pn ,@paramCode := ib.paramCode pc FROM (SELECT * from temp_param_result order by paramCode, id) ib, ( SELECT @ROW := 1, @pname := '', @paramCode := '' ) row1 ) aa GROUP BY aa.paramCode,aa. resultStr; -----------------------------------------------------oracle 解决方案------------------------------------------

select te.paramCode, max(case when te.resultStr = te.next_result or te.next_result='X' then te.next_rn - te.rn + 1 else te.next_rn - te.rn - 1 end) cnt from (select tx.paramCode, tx.resultStr, tx.next_result, tx.rn, lead(tx.rn, 1, tx.rn) over(partition by tx.paramCode order by tx.rn) next_rn from (select t.paramCode, t.resultStr, row_number() over(partition by t.paramCode order by t.id) rn, lag(t.resultStr, 1, 'X') over(partition by t.paramCode order by t.id) prev_result, lead(t.resultStr, 1, 'X') over(partition by t.paramCode order by t.id) next_result from temp_param_result t) tx where tx.rn='1' or (tx.prev_result <> tx.next_result) ) te group by te.paramCode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值