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

【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.lryc.cn/news/257655.html

相关文章:

  • CLIP在Github上的使用教程
  • 入职字节外包一个月,我离职了。。。
  • SpringBoot的web开发
  • 传染病传播速度
  • 前端打包环境配置步骤
  • css的4种引入方式--内联样式(标签内style)、内部样式表(<style>)、外部样式表(<link>、@import)
  • GPT-4 变懒了?官方回复
  • 编译器和 IR:LLVM IR、SPIR-V 和 MLIR
  • 蓝牙物联网对接技术难点有哪些?
  • 漫谈Uniapp App热更新包-Jenkins CI/CD打包工具链的搭建
  • Axure简单安装与入门
  • 前端知识笔记(四十五)———前端开发与后端开发有什么区别
  • Jol-分析Java对象的内存布局
  • 基于sfunction builder的c-sfunction编写及案例测试分析
  • 【Java期末复习资料】(1)知识点总结
  • 进程、容器与虚拟机的区别
  • 全网快递批量查询的得力助手
  • uniapp开发小程序经验记录
  • PR自动剪辑视频工具AI智能剪辑插件AutoPod
  • Visual Studio 2022+Python3.11实现C++调用python接口
  • 10天玩转Python第2天:python判断语句基础示例全面详解与代码练习
  • 2024年网络安全竞赛-网站渗透
  • kafka学习笔记--基础知识概述
  • 聊聊AsyncHttpClient的KeepAliveStrategy
  • 视频推拉流直播点播EasyDSS平台点播文件加密存储的实现方法
  • LVGL——按钮部件
  • RE2文本匹配调优实战
  • Java - 线程间的通信方式
  • 【计算机网络】HTTP响应报文Cookie原理
  • 2023年度盘点:智能汽车、自动驾驶、车联网必读书单