连续三天的人数

-- 连抽3天的用户人数 

select count(distinct T2.F_PHONE)
from (
       select T1.F_PHONE
       from (
              select
                T.F_PHONE,
                trunc(T.F_AWARD_TIME - row_number()
                over (
                  partition by T.F_PHONE
                  order by T.F_AWARD_TIME )) rn
              from TB_BIGTURNTABLE_RECORD T
              where T.F_AWARD_TIME >= to_date('20190121000000', 'yyyyMMddHH24miss') and
                    T.F_AWARD_TIME >= to_date('20190121230000', 'yyyyMMddHH24miss') and T.F_STATUS != 0 and
                    T.F_STATUS != 7
            ) T1
       group by T1.F_PHONE, T1.rn
       having count(1) >= 3
     ) T2;

1, trunc() avoids the problem of the juncture between two years. For example, 20190101 - 2 is 20190099 and 20190101 - 1 is 20190100, but not the last day of year 2018. Using trunc() you get the last day of the last year.

2, over(), partition by and order by act only on the results filtered by where, group by and having.

3, row_number() is the rank of the current object in the results of over(), partition by and order by. For example, 2's rank is 1 in the result set [1, 2, 3].

4, by subtracting from the value of the object the rank of the object, and using group by at the end, we get continuous values.

For example,

Value, Rank, Value - Rank

1, 0, 1

2, 1, 1

3, 2, 1

You see, the continous values have the same Value - Rank!

猜你喜欢

转载自blog.csdn.net/qq_25527791/article/details/87525936