在另一列中满足特定条件之前(基于时间)仅保留一列中每个值的行
Only keep rows per each value in one column BEFORE (time-based) a certain condition is fulfilled in another column
我有 table 个订单,编号为 order_id
。其他相关列包括 customer_id
、timestamp
和 Condition_column
。对于每个 customer_id
,我只想在第一次 Condition_column
不是 null
.
之前保留行(又名 timestamp<=
)
我的数据服务器是 presto,我相信这应该可以通过一些 OVER PARTITION BY
语句实现;但我不知道怎么办。
这是一个例子table:
order_id customer_id timestamp Condition_col
abc stan 5/11/19
def stan 5/20/19
efg stan 6/1/19 text
hij stan 6/9/19 text2
jkl jimmy 5/22/19 text3
klm mike 5/01/19
lmn mike 5/17/19
xyz mike 5/30/19 text4
wyt sam 5/4/19 text5
ard sam 5/24/19 text6
shd sam 6/5/19 text7
这是我想要的结果:
order_id customer_id timestamp Condition_col
abc stan 5/11/19
def stan 5/20/19
efg stan 6/1/19 text
jkl jimmy 5/22/19 text3
klm mike 5/01/19
lmn mike 5/17/19
xyz mike 5/30/19 text4
wyt sam 5/4/19 text5
我有点想弄清楚如何从逻辑上写这个。任何帮助将不胜感激。
您可以使用 window 函数:
select t.*
from (select t.*,
min(case when condition_col is not null then timestamp end) over (partition by customer_id) as min_condition_ts
from t
) t
where min_condition_ts is null or -- no non-NULL value
timestamp <= min_condition_ts;
我有 table 个订单,编号为 order_id
。其他相关列包括 customer_id
、timestamp
和 Condition_column
。对于每个 customer_id
,我只想在第一次 Condition_column
不是 null
.
timestamp<=
)
我的数据服务器是 presto,我相信这应该可以通过一些 OVER PARTITION BY
语句实现;但我不知道怎么办。
这是一个例子table:
order_id customer_id timestamp Condition_col
abc stan 5/11/19
def stan 5/20/19
efg stan 6/1/19 text
hij stan 6/9/19 text2
jkl jimmy 5/22/19 text3
klm mike 5/01/19
lmn mike 5/17/19
xyz mike 5/30/19 text4
wyt sam 5/4/19 text5
ard sam 5/24/19 text6
shd sam 6/5/19 text7
这是我想要的结果:
order_id customer_id timestamp Condition_col
abc stan 5/11/19
def stan 5/20/19
efg stan 6/1/19 text
jkl jimmy 5/22/19 text3
klm mike 5/01/19
lmn mike 5/17/19
xyz mike 5/30/19 text4
wyt sam 5/4/19 text5
我有点想弄清楚如何从逻辑上写这个。任何帮助将不胜感激。
您可以使用 window 函数:
select t.*
from (select t.*,
min(case when condition_col is not null then timestamp end) over (partition by customer_id) as min_condition_ts
from t
) t
where min_condition_ts is null or -- no non-NULL value
timestamp <= min_condition_ts;