1. 程式人生 > >oracle 連續日期資料統計

oracle 連續日期資料統計

 -- 取 使用者ID 連續3天以上的記錄

-- 資料庫存的是“5/1/2015 16:26:43 ” 要轉化 to_char(B.start_time,'yyyymmdd')

-- 獲得使用者ID

 SELECT C.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