mysql連續打卡次數 mysql 統計連續天數

2021-10-17 12:32:38 字數 1578 閱讀 5216

以下為例子資料 圖1

圖1首先根據要求取出beforemeal要在7.0以下 並且 bingaftermeal要在11.1以下

select accountid,createtime from diabetesrecord where beforemeal < 7.0 and aftermeal < 11.1 group by accountid,createtime

得到以下資料如圖2

圖2把連續的天數進行分組

select *,((select count(1) from (select accountid,createtime from diabetesrecord where beforemeal < 7.0 and aftermeal < 11.1 group by accountid,createtime)dr2 where dr1.accountid = dr2.accountid and dr2.createtime <= dr1.createtime) - day(dr1.createtime)) as 'rownum' from (select accountid,createtime from diabetesrecord where beforemeal < 7.0 and aftermeal < 11.1 group by accountid,createtime)dr1

結果如圖3所示

圖3按使用者 、開始時間、結束時間 和 連續天數分組

select accountid,min(createtime),max(createtime),(datediff(max(createtime),min(createtime))+1) as 'rn' from

(select *,((select count(1) from (select accountid,createtime from diabetesrecord where beforemeal < 7.0 and aftermeal < 11.1 group by accountid,createtime)dr2 where dr1.accountid = dr2.accountid and dr2.createtime <= dr1.createtime) - day(dr1.createtime)) as 'rownum' from (select accountid,createtime from diabetesrecord where beforemeal < 7.0 and aftermeal < 11.1 group by accountid,createtime)dr1)z group by accountid,rownum

得到如圖4所示

圖4

mysql 最大連續出現次數 連續出現最長次數

輸入第一行有乙個整數n n 1000 為陣列元素的個數。第二行有n個整數,整數之間以乙個空格分開。輸出輸出最長平台的長度。樣例輸入 1 2 2 3 3 3 4 5 5 6 樣例輸出 include using namespace std int main int a 1001 n cin n for...

mysql實現連續 累計次數的邏輯

最近碰到這樣乙個需求,測算連續逾期3期或者累計逾期6期的件,並找到觸發這個條件是在哪一期。實現的關鍵是group contact函式。首先給每期還款狀態打標flag,1逾期,0不逾期。select group contact flag order by 期數 asc separator as fla...

Mysq篇 了解Mysql(一)

in關鍵字和exists關鍵字select from a where a.idin select id from b 對外表a使用索引效率高,建議a為大表。select from a whereexists select from b where a.id b.id 對內表b使用索引效率高,建議b為...