题解 | #某宝店铺连续2天及以上购物的用户及其对应的天数#

某宝店铺连续2天及以上购物的用户及其对应的天数

https://www.nowcoder.com/practice/63ac3be0e4b44cce8dd2619d2236c3bf

# 一个用户在一天可能会有多个购买记录,需要去重
with t1 as (
    select DISTINCT user_id, sales_date
    from sales_tb
),
t2 as (
    select user_id, sales_date,
    (case when LAG(sales_date, 1, 1) over(partition by user_id order by sales_date) = 1 then 1 else timestampdiff(day, LAG(sales_date) over(partition by user_id order by sales_date), sales_date) end) as grp
    from t1
)
select user_id, count(sales_date) as days_count
from t2
where grp = 1
group by user_id
having days_count >= 2;

全部评论

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务