如何查找第n个值是否小于先前的n-1值

时间:2018-04-30 23:19:52

标签: sql hive

我有一个包含以下架构的表:

Order_id    customer_Id purchaseDate    movie_Id    minutesStreamed
01  C1  1/1/2000    P1  100
02  C2  1/1/2002    P2  90
03  C3  4/1/2002    P3  93
04  C4  4/1/2003    P1  99
05  C4  1/1/2006    P2  99
06  C1  5/1/2006    P5  89
07  C4  12/1/2017   P5  89
08  C3  3/3/2018    P1  145
09  C4  3/3/2018    P6  147

我想找到那些每次观看电影时观看时间越来越少的客户,即他们的第二次流式播放时间小于1次,而他们的第3次播放次数少于第2次,依此类推等等。

我知道如何找到一个案例,即3< 2rd或2nd< 1st但是如何检查所有组合。

select a.*
from
(
select customer_id,purchase_date,minutes_streamed, lag(minutes_streamed,1) over (partition by customer_id order by purchase_date) prev_mins_streams
from orders
)a
inner join
(select customer_id,max(purchase_date) max_purchase_dt from orders group by customer_id) b
on a.customer_id=b.customer_id
and a.purchase_date=b.max_purchase_dt
where a.minutes_streamed<a.prev_mins_streams
;

1 个答案:

答案 0 :(得分:1)

如果您需要仅拒绝的客户,请定义一个标志,然后汇总该单位:

select o.customer_id
from (select o.*,
             lag(minutes_streamed,1) over (partition by customer_id order by purchase_date) as prev_ms
      from orders o
     ) o
group by o.customer_id
having sum(case when prev_ms is null or prev_ms < minutes_streams then 0 else 1 end) = 0; 

having子句基本上计算异常。 = 0表示没有。