1225 報告系統狀態的連續日期

2022-08-20 07:27:16 字數 3952 閱讀 1256

table:failed

+--------------+---------+

| column name | type |

+--------------+---------+

| fail_date | date |

+--------------+---------+

該錶主鍵為 fail_date。

該錶包含失敗任務的天數.

table:succeeded

+--------------+---------+

| column name | type |

+--------------+---------+

| success_date | date |

+--------------+---------+

該錶主鍵為 success_date。

該錶包含成功任務的天數.

系統 每天 執行乙個任務。每個任務都獨立於先前的任務。任務的狀態可以是失敗或是成功。

編寫乙個 sql 查詢 2019-01-01 到 2019-12-31 期間任務連續同狀態period_state的起止日期(start_dateend_date)。即如果任務失敗了,就是失敗狀態的起止日期,如果任務成功了,就是成功狀態的起止日期。

最後結果按照起始日期start_date排序

查詢結果樣例如下所示:

failed table:

+-------------------+

| fail_date |

+-------------------+

| 2018-12-28 |

| 2018-12-29 |

| 2019-01-04 |

| 2019-01-05 |

+-------------------+

succeeded table:

+-------------------+

| success_date |

+-------------------+

| 2018-12-30 |

| 2018-12-31 |

| 2019-01-01 |

| 2019-01-02 |

| 2019-01-03 |

| 2019-01-06 |

+-------------------+

result table:

+--------------+--------------+--------------+

| period_state | start date | end date |

+--------------+--------------+--------------+

| present | 2019-01-01 | 2019-01-03 |

| missing | 2019-01-04 | 2019-01-05 |

| present | 2019-01-06 | 2019-01-06 |

+--------------+--------------+--------------+

結果忽略了 2018 年的記錄,因為我們只關心從 2019-01-01 到 2019-12-31 的記錄

從 2019-01-01 到 2019-01-03 所有任務成功,系統狀態為 "succeeded"。

從 2019-01-04 到 2019-01-05 所有任務失敗,系統狀態為 "failed"。

從 2019-01-06 到 2019-01-06 所有任務成功,系統狀態為 "succeeded"。

couchpotato613

發布於 1 個月前

109 閱讀

我是這樣想的:

先按照日期遞增的順序把失敗的和成功的天數連續查出來,sql如下:

select date,period_state from (

select fail_date as date,'failed' as period_state from failed

where fail_date between '2019-01-01' and '2019-12-31'

union all

select success_date as date,'succeeded' as period_state from succeeded

where success_date between '2019-01-01' and '2019-12-31'

) t1 order by t1.date as

然後按照連續天數的狀態給個排名rank,連續的狀態相同的rank一樣,不一樣的rank遞增,

sql如下:

select date,period_state,

if(@prev = period_state ,@tmp := @tmp, @tmp := @tmp + 1) as rank,

@prev := period_state as prev from

(select date,period_state from (

select fail_date as date,'failed' as period_state from failed

where fail_date between '2019-01-01' and '2019-12-31'

union all

select success_date as date,'succeeded' as period_state from succeeded

where success_date between '2019-01-01' and '2019-12-31'

) t1 order by t1.date asc) t2,(select @prev := null,@tmp := 0) as init

得到上述結果之後,再按照rank分組,把每一組的最大值和最小值分別作為start_date和end_date,

完整的sql如下:

select t3.period_state as period_state,min(date) as start_date,

max(date) as end_date from

(select date,period_state,

if(@prev = period_state ,@tmp := @tmp, @tmp := @tmp + 1) as rank,

@prev := period_state as prev

from

(select date,period_state from (

select fail_date as date,'failed' as period_state from failed

where fail_date between '2019-01-01' and '2019-12-31'

union all

select success_date as date,'succeeded' as period_state from succeeded

where success_date between '2019-01-01' and '2019-12-31'

) t1 order by t1.date asc) t2,(select @prev := null,@tmp := 0) as init) t3

group by rank

ps 報告當前系統的程序狀態

ps aux最初用到unix style中,而ps ef被用在system v style中,兩者輸出略有不同。現在的大部分linux系統都是可以同時使用這兩種方式的。linux上程序有5種狀態 執行 正在執行或在執行佇列中等待 中斷 休眠中,受阻,在等待某個條件的形成或接受到訊號 不可中斷 收到訊...

python 連續輸出日期 我想生成連續的日期

我有這樣的 import time from datetime import date startyear raw input start year startmonth raw input start month startday raw input start day endyear raw i...

Sql Server 生成連續日期的方法總結

第一種,利用master.spt values來生成連續日期 sql view plain copy print declare date datetime set date 2009 02 05 select day convert varchar dateadd mm,datediff mm,0...