题解 | #某宝店铺连续2天及以上购物的用户及其对应的天数#
某宝店铺连续2天及以上购物的用户及其对应的天数
http://www.nowcoder.com/practice/63ac3be0e4b44cce8dd2619d2236c3bf
经典的求连续天数问题
统一的思路都是先按天数进行排序,再用天数减去排名进行count得到连续登录的天数
with t as (select
distinct user_id,
sales_date,
dense_rank() over(partition by user_id order by sales_date) rk
from sales_tb)
select
user_id,
count(sales_date - rk)
from t
group by user_id
having count(sales_date - rk) >= 2
order by user_id