zoukankan      html  css  js  c++  java
  • LeetCode 【困难】数据库-第1225:报告系统状态的连续日期

    题目

    数据

    结果

    解答

    1.union 合并数据、筛选日期

            select 'succeeded' as 'state',success_date as 'dt'
            from Succeeded where success_date between '2019-01-01' and '2019-12-31'
            union 
            select 'failed'as 'state',fail_date as 'dt'
            from Failed    where fail_date    between '2019-01-01' and '2019-12-31'
    

    2.根据状态排名,该状态对应的日期减去排名,如果相同,则可以认为日期是连续的。subdate函数(date,a)-->date-a

    3.最后,挑选出来,最小(start)、最大(end) 日期,然后按照状态分组、按照subdate函数的结果分组。

    select state as period_state,   min(dt ) as start_date,    max(dt) as end_date
    from
    (
        select *,rank()over(partition by state order by  dt ) as rk ,subdate(dt,rank()over(partition by state order by  dt)) as dif
        from 
        (
            select 'succeeded' as 'state',success_date as 'dt'
            from Succeeded where success_date between '2019-01-01' and '2019-12-31'
            union 
            select 'failed'as 'state',fail_date as 'dt'
            from Failed    where fail_date    between '2019-01-01' and '2019-12-31'
        )t1
    )t2
    group by state,dif
    order by dt
    
    

  • 相关阅读:
    MQTT的编译和安装(mosquitto)
    四、固件分析-固件逆向
    三、固件分析-固件提取
    二、硬件分析-电路分析
    一、硬件分析
    gdb调试
    AES加密中遇到的坑
    多线程中快速定位段错误位置
    理想的数据加密流程
    Centos进入单用户修改root密码
  • 原文地址:https://www.cnblogs.com/Tdazheng/p/14981696.html
Copyright © 2011-2022 走看看