oracle 連續日期統計

2021-07-02 04:06:53 字數 2184 閱讀 8544

select t2.user_id,t2.start_time, t2.score ,t2.win_coins from (

select d.user_id,d.start_time, d.score ,d.win_coins  from (

select user_id ,to_char(start_time,'yyyymmdd')as start_time,score,win_coins

, row_number() over (partition by user_id,to_char(start_time,'yyyymmdd') order by to_date(to_char(start_time,'yyyymmdd'),'yyyymmdd')asc,score asc) as rn

from drivereport where to_char(start_time,'yyyy-mm-dd') between '2015-05-01' and '2015-05-11'

) d where rn=1) t2,

( select distinct(c.user_id) as user_id  from

(select b.user_id, (max (to_char(b.start_time,'yyyymmdd')) - min (to_char(b.start_time,'yyyymmdd'))+1) as days

from (select a.*, to_number(to_char(a.start_time,'yyyymmdd') - rownum)as days

from (

select user_id,  to_date(to_char(start_time,'yyyymmdd'),'yyyymmdd') as start_time ,min(score)as mi

from drivereport where to_char(start_time,'yyyymmdd') >='20150501' and to_char(start_time,'yyyymmdd') <= '20150511'

group by user_id, to_char(start_time,'yyyymmdd') order by user_id, to_char(start_time,'yyyymmdd')

) a  ) b

group by b.user_id, b.days) c

where c.days > 2 order by c.user_id asc) t3

where  t3.user_id = t2.user_id

-- 獲得至少連續3天開啟駕駛記錄 例如 使用者1:1號~3號的記錄,使用者2:2號~4號的記錄,使用者3: 4號~6號的記錄 ;使用者4:7號~10號的記錄

-- 下面的sql主要獲得至少連續3天的有哪些使用者的id

select distinct(c.user_id) as user_id

from (select b.user_id

,(max(to_char(b.start_time, 'yyyymmdd')) -

min(to_char(b.start_time, 'yyyymmdd')) + 1) as days

from (select a.user_id

,a.start_time

,to_number(to_char(a.start_time, 'yyyymmdd') -

rownum) as days

from (select user_id

,to_date(to_char(start_time, 'yyyymmdd'), 'yyyymmdd') as start_time

from drivereport

where to_char(start_time, 'yyyy-mm-dd') >=

'2015-05-01'

and to_char(start_time, 'yyyy-mm-dd') <=

'2015-05-11'

group by user_id

,to_char(start_time, 'yyyymmdd')

order by user_id

,to_char(start_time, 'yyyymmdd')) a) b

group by b.user_id

,b.days) c

where c.days > 2

order by c.user_id asc

Oracle連續相同資料的統計

有些事情始終是需要堅持下去的。今天覆習一下之前用到的連續相同資料的統計。首先,建立乙個簡單的測試表,這裡過程就略過了,直接上表 真的是以簡單為主,哈哈 第一種寫法row number select val,count from select id,val,row number over order ...

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

以下為例子資料 圖1 圖1首先根據要求取出beforemeal要在7.0以下 並且 bingaftermeal要在11.1以下 select accountid,createtime from diabetesrecord where beforemeal 7.0 and aftermeal 11....

MYSQL 常用日期統計

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 近7天 select from 表名 where date sub curdate interv...