提问者:小点点

如何识别特定偶数之前出现次数


我有一个回头客的数据集,这些回头客每次购买一种产品,并想知道他们购买了多少次,直到他们购买了一个特定的物品。 我有客户id,访问时间戳,和购买作出。


共1个答案

匿名用户

如果我理解正确,您可以使用窗口函数和聚合:

select customer_id, product_ts, count(*) as num_products_before
from (select t.*,
             min(case when product = :product then timestamp end) over (partition by customerid) as product_ts
      from t
     ) t
where timestamp < product_ts
group by customer_id, product_ts;