oracle 连续15天,求教:统计用户连续活跃天数的sql

本帖最后由 cs4680ok 于 2015-9-24 17:37 编辑

思路比较乱,但是可以实现:

create table test1 as

with t1 as

(select '1' as u_id,

to_date('2015-6-1 8:20:00', 'yyyy-mm-dd hh24:mi:ss') as login_time

from dual

union all

select '1', to_date('2015-6-2 7:20:05', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '1', to_date('2015-6-3 21:20:30', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '2', to_date('2015-6-1 8:10:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '2', to_date('2015-6-3 8:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '2', to_date('2015-6-4 18:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '1', to_date('2015-6-5 9:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '1', to_date('2015-6-6 16:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '3', to_date('2015-6-1 8:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '1', to_date('2015-6-7 12:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '1', to_date('2015-6-8 23:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '3', to_date('2015-6-2 8:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual

union all

select '3', to_date('2015-6-3 2:20:00', 'yyyy-mm-dd hh24:mi:ss')

from dual)

select * from t1;

create view t1 as

select t1.u_id,

to_char(t1.login_time, 'yyyymmdd') begin_day,

to_char(t2.login_time, 'yyyymmdd') next_day

from test1 t1, test1 t2

where t1.u_id = t2.u_id

and to_char(t1.login_time, 'yyyymmdd') =

to_char(t2.login_time - 1, 'yyyymmdd')

select u_id, max(next_day - begin_day)+1

from (select u_id,

min(begin_day) begin_day,

max(next_day) next_day

from (select u_id,

begin_day,

next_day,

sum(flag) over(partition by u_id order by begin_day) gr

from (select u_id,

begin_day,

next_day,

lag(next_day) over(partition by u_id order by next_day),

case

when lag(next_day)

over(partition by u_id order by

next_day) = begin_day then

0

else

1

end flag

from t1))

group by u_id, gr)

group by u_id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值