当前位置: 首页 > article >正文

【mysql】下一行减去上一行数据、自增序列场景应用

背景

想获取if_yc为1连续账期数据
在这里插入图片描述

思路

  1. 获取所有if_yc为1的账期数据
  2. 下一行减去上一行账期,如果为1则为连续,不等于1就为断档
  3. 获取不等于1的最小账期,就是离当前账期最近连续账期

代码

以下为mysql语法:

select acct_month
from(
select (@m:=@m+1) rn,acct_month
from(
select '202311' acct_month,1 if_yc
union all 
select '202310',1
union all 
select '202309',1
union all 
select '202308',0
union all 
select '202307',1
union all 
select '202306',1) a,(select @m:=0) b
where if_yc = 1 
order by acct_month desc 
) m ,
(select min(t1.rn)+1 rn1-- ,t1.acct_month-t2.acct_month
from (
select (@i:=@i+1) rn,acct_month
from(
select '202311' acct_month,1 if_yc
union all 
select '202310',1
union all 
select '202309',1
union all 
select '202308',0
union all 
select '202307',1
union all 
select '202306',1) a,(select @i:=0) b
where if_yc = 1 
order by acct_month desc ) t1 ,
(
select (@j:=@j+1) rn,acct_month
from(
select '202311' acct_month,1 if_yc
union all 
select '202310',1
union all 
select '202309',1
union all 
select '202308',0
union all 
select '202307',1
union all 
select '202306',1) a,(select @j:=-1) b
where if_yc = 1 
order by acct_month desc 
) t2 where t1.rn = t2.rn and t1.acct_month-t2.acct_month <> 1 ) n 
where rn < n.rn1

http://www.kler.cn/news/161909.html

相关文章:

  • 2023年4K投影仪怎么选?极米H6 4K高亮版怎么样?
  • Leetcode—1466.重新规划路线【中等】
  • 【PTA题目】7-7 自守数 分数 15
  • 芯知识 | 如何选择合适的单片机语音芯片?
  • 使用单例模式+观察者模式实现参数配置实时更新
  • 算术运算(这么简单?进来坐坐?)
  • 复杂gRPC之go调用go
  • C++标准模板(STL)- 类型支持 (杂项变换,确定一组类型的公共类型,std::common_type)
  • C#-using处理非托管资源
  • 我不是DBA之慢SQL诊断方式
  • 云原生之深入解析Kubernetes策略引擎对比:OPA/Gatekeeper与Kyverno
  • 【React】路由的基础使用
  • SpringAOP专栏一《使用教程篇》
  • 学习Opencv(蝴蝶书/C++)相关1——4.图形和大型数组类型
  • Java String相关问题
  • StringBoot常用注解(不断添加)
  • ncnn模型部署——使用VS2019把项目打包成DLL文件
  • 【头歌实训】分布式文件系统 HDFS
  • 火狐,要完了!
  • GateWay网关介绍以及整合knife4j聚合所有服务的接口文档
  • pymol使用
  • EI级 | Matlab实现TCN-GRU-Multihead-Attention多头注意力机制多变量时间序列预测
  • TE发布最新TC Policy 3.1
  • 单片机第三季-第四课:STM32下载、MDK和调试器
  • 网站建设app开发小程序制作|企业软件定制
  • 有爱的冬天不再冷——壹基金儿童温暖包抵达富平
  • Kubernetes(K8s)Pod控制器详解-06
  • Java的List中的各种浅拷贝和深拷贝问题
  • 超大规模集成电路设计----FPGA时序模型及FSM的设计(八)
  • 查看端口号是否被占用