首页 > 数据库 >【LeetCode1225. 报告系统状态的连续日期】MySQL使用lag,lead得到连续段的:开始标志,结束标志,分组号,长度

【LeetCode1225. 报告系统状态的连续日期】MySQL使用lag,lead得到连续段的:开始标志,结束标志,分组号,长度

时间:2023-08-19 11:33:05浏览次数:47  
标签:标志 status task end lead lag date select

目录

题目地址

https://leetcode.cn/problems/report-contiguous-dates/description/

题目描述

A system is running one task every day. Every task is independent of the previous tasks. The tasks can fail or succeed.

Write a solution to report the period_state for each continuous interval of days in the period from 2019-01-01 to 2019-12-31.

period_state is 'failed' if tasks in this interval failed or 'succeeded' if tasks in this interval succeeded. Interval of days are retrieved as start_date and end_date.

Return the result table ordered by start_date.

代码

# Write your MySQL query statement below

with t1 as(
    select "failed" as status, fail_date as task_date from Failed 
    union all
    select "succeeded" as status, success_date as task_date from Succeeded
)
,
t2 as (
    select * from t1 
    where task_date between date("2019-01-01") and date("2019-12-31")
    order by task_date asc
)
,
t3 as (
    select *, 
    lag(task_date, 1, task_date -interval 2 day) over(order by task_date) as lag_task_date,
    lag(status, 1) over(order by task_date) as lag_status,
    lead(task_date, 1, task_date + interval 2 day) over(order by task_date) as lead_task_date,
    lead(status, 1) over(order by task_date) as lead_status
    from t2
)
,
Segments as(
    select *,
    (case when datediff(task_date, lag_task_date) >1 or status<>lag_status then 1 else 0 end) as is_start ,
    (case when datediff(lead_task_date, task_date)>1 or status<>lead_status then 1 else 0 end) as is_end
    from t3
)
,
GroupsWithNumber AS (
    SELECT *,
           SUM(is_start) OVER (ORDER BY task_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS group_num
    FROM Segments
)
,
comprehensive_statistic as(
    SELECT *,
    COUNT(task_date) OVER (PARTITION BY group_num) AS segment_length
    FROM GroupsWithNumber
    ORDER BY task_date
)

# select * from comprehensive_statistic

select period_state , start_date  , end_date from
(select status as period_state, group_num, min(case when is_start then task_date end) as start_date,
max(case when is_end then task_date end) as end_date
from comprehensive_statistic
group by status,group_num) tmp1
order by start_date asc

标签:标志,status,task,end,lead,lag,date,select
From: https://www.cnblogs.com/yhm138/p/17642230.html

相关文章

  • 研发团队绩效考核:Leader 如何做到赏罚分明?
    今天话题的主题是Leader如何在绩效考核中做到赏罚分明?作者是苏宁金科CTO肖军要想做到赏罚分明就需要先明确:什么人因为做了什么事,基于什么量化指标和规则,获得什么权益或处罚。技术考核很难落地,有三个原因难以找到关键指标团队成员难以分配绩效权重考核结果难以强应用难以找到关键......
  • 中兴交换机MC-LAG配置实例
    配置说明MC-LAG是一种实现跨设备链路聚合的机制,能够实现多台设备间的链路聚合,从而把链路可靠性从单板级提高到了设备级,组成双活系统。MC-LAG典型组网如图1所示:图1  MC-LAG典型组网示意图 配置思路 创建SmartGroup及配置peerlink链路 在peerlink上配置静态路......
  • MySQL 使用表的自联结,lag,lead得到该行记录所在连续段长度
    目录题目地址代码题目地址https://leetcode.cn/problems/human-traffic-of-stadium/description/代码##WriteyourMySQLquerystatementbelow##本质上就是连续签到问题呗#SELECTVersion()#8.0.33,用户变量编程用不了witht1as(SELECT*fromstadium......
  • Go中flag用法
    flag包实现了命令行标志解析。使用flag.String(),Bool(),Int()等定义标志。这声明了一个整数标志-n,存储在指针nFlag中,类型为*int:import"flag"varnFlag=flag.Int("n",1234,"helpmessageforflagn")如果您愿意,您可以使用Var()函数将标志绑定到一个变量。......
  • Go语言中的flag
    Go语言的flag包提供了一个简单的方式来定义和解析命令行参数。以下是如何使用flag包的基本步骤和示例:1.定义参数使用flag包的相关函数来定义命令行参数。例如,你可以使用StringVar、IntVar和BoolVar来定义字符串、整数和布尔值参数。2.调用flag.Parse()这将解析......
  • 基于YOLOv3的交通标志检测的实现和测试​
    1搭建环境1.1YOLO实践应用之搭建开发环境Anaconda3Anaconda是一个开源的Python语言发行版及其包管理器,在数据科学、机器学习和科学计算领域广受欢迎。Anaconda使用Conda进行包管理。Conda可以创建虚拟环境来隔离不同项目所需的包和依赖,并可以方便地提交代码和环境设置,它包含了众......
  • 交换机M-LAG知识小结
    M-LAG(MultichassisLinkAggregationGroup)即跨设备链路聚合组,是一种实现跨设备链路聚合的机制,将一台设备与另外两台设备进行跨设备链路聚合,从而把链路可靠性从单板级提高到了设备级,组成双活系统M-LAG的作用:1、增加带宽:将成员交换机的多条物理链路配置成一个聚合组,提高交换机的上行......
  • Yocto SDK 修改环境加载脚本中的编译选项flags
    修改build/conf/local.conf,增加或者覆盖如下变量DEBUG_BUILD="0"DEBUG_FLAGS=""FULL_OPTIMIZATION="-O3-pipe"这些变量原始定义于poky/meta/conf/bitbake.conf609行附近......
  • 2023 *CTF flagfile
    flagfile格式文件是mgc,题目提示用file命令查看观察后,忽略有规律的,取出没规律的将红圈的数字异或,得到第一组数据这里发现后面是ffff,从这里隔开,异或的数据作为第二组异或的数据都将其转为十进制后,发现第二组可能是ascII编码,转化得到:f_o_a__lhy_s_y^^hete_ug___goo_t_第一......
  • 具备低功耗的S25FL256LAGMFM000、S25FL256LAGMFA003、S25FL256LAGMFA001(256Mb)NOR闪存
    FL-LNOR闪存系列具备低功耗和AEC-Q100汽车认证,并且能够在扩展温度范围内提供更高的读带宽和更快的编程速度。借助小巧、统一的4KB物理存储单元,该系列闪存产品能够以最佳方式存储程序代码和参数数据,是高级辅助驾驶系统(ADAS)、汽车仪表盘及信息娱乐系统、工业控制及智能工厂设备、网......