oracle中获取连续几天最低数据

WITH order_rank AS(select
                   ROW_NUMBER() over (partition BY tt.area_group ORDER BY tt.date_id) newRn,TO_DATE(tt.DATE_ID,'yyyy-MM-dd') date_time,
                   tt.* from (
                     SELECT
                         ROW_NUMBER() over (partition BY t.area_group, t.date_id ORDER BY t.date_id,t.v13_kpi_value04 asc )rn,
                             t.*
                     FROM
                         (select dim.area_group,jy.* from dim_area_group dim
                           left join jy_d_jcfzbp_zone jy on jy.city_id=dim.area_id and dim.area_group is not null
                          where date_id like concat(concat('%', #{dateId}), '%') and cust_kind='ALL' and county_id is null)t
                  )tt where rn =1)

select t1.* from (
     SELECT
         to_char(sysdate - 1, 'mm"月"dd"日"') as "yesDate",
         ROW_NUMBER() over (partition BY ulr.area_group ORDER BY COUNT(ulr.area_group) desc)rn,
         ulr.area_group county_group,
         ulr.city_id county_id,
         ulr.city_name remark,
         (ulr.date_time-newRn) date_group,
         MIN(ulr.date_time),
         MAX(ulr.date_time),
         COUNT(ulr.area_group) last_days
     FROM order_rank ulr
     GROUP BY ulr.area_group,ulr.city_id,ulr.city_name,(ulr.date_time-newRn)
 )t1 where rn=1

####参考文章

https://blog.csdn.net/weixin_45875358/article/details/131472993

01 题目

登陆表中有 id(user_id)、login_time求出用户连续登录天数

02 建表插入数据

CREATE TABLE "FINANCE"."USER_LOGIN"
 (
 id VARCHAR2 ( 255 BYTE ),
	login_time DATE
	)
 
 
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-01 01:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-01 02:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-01 03:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-02 04:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-03 06:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-07 08:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-08 22:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-09 08:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-10 01:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('A', TO_DATE('2023-06-11 02:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-01 01:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-02 02:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-03 00:50:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-04 00:50:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-05 10:20:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-06 20:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-10 10:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-18 10:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
INSERT INTO "FINANCE"."USER_LOGIN" VALUES ('B', TO_DATE('2023-06-20 10:00:00', 'SYYYY-MM-DD HH24:MI:SS'));
WITH user_log_rank   AS(SELECT
	ul1.id,
	ul1.time,
	ROW_NUMBER() over ( partition BY ul1.id ORDER BY ul1.time ) rn
FROM
	( SELECT ul.id, TRUNC( ul.login_time ) time FROM USER_LOGIN ul GROUP BY ul.id, TRUNC( ul.login_time )) ul1)
	
SELECT
ulr.id,
(ulr.time-rn) date_group,
MIN(ulr.time),
MAX(ulr.time),
COUNT(ulr.id) continue_days
 
FROM user_log_rank ulr
GROUP BY ulr.id,
(ulr.time-rn)

oracle中获取连续几天最低数据_第1张图片

3.4 取用户最大值 去重 即得用户最大连续登陆天数

WITH user_log_rank   AS(SELECT
	ul1.id,
	ul1.time,
	ROW_NUMBER() over ( partition BY ul1.id ORDER BY ul1.time ) rn
FROM
	( SELECT ul.id, TRUNC( ul.login_time ) time FROM USER_LOGIN ul GROUP BY ul.id, TRUNC( ul.login_time )) ul1)
	
 
SELECT 
ulr1.id,
MAX(ulr1.continue_days) max_continue_days
 
FROM(SELECT
ulr.id,
(ulr.time-rn) date_group,
MIN(ulr.time),
MAX(ulr.time),
COUNT(ulr.id) continue_days
 
FROM user_log_rank ulr
GROUP BY ulr.id,
(ulr.time-rn))ulr1
GROUP BY ulr1.id

oracle中获取连续几天最低数据_第2张图片

你可能感兴趣的:(oracle,数据库,sql)