我有一個資料框架:
id value
a1 0
a1 1
a1 2 2
a1 3 3
a2 0 0
a2 1 1
a3 0 0
a3 1 1
a3 2 2
a3 3
我想過濾id,只留下那些值從0到3(0,1,2,3)的。所以在這個例子中,id a2必須被洗掉,因為它只有0和1的值。
id value。
a1 0
a1 1
a1 2 2
a1 3 3
a3 0 0
a3 1 1
a3 2 2
a3 3
如何做到這一點?
uj5u.com熱心網友回復:
一個簡單的聚合方法是:
SELECT id, value
FROM yourTable
WHERE id IN (
SELECT id
FROM yourTable
WHERE value IN (0, 1, 2, 3)
GROUP BY id
HAVING COUNT(DISTINCT value) = 4
);
uj5u.com熱心網友回復:
我建議使用視窗函式,特別是如果你使用Presto。 假設這些行只有這四個值,并且這些行是唯一的:
select t.*
from (select t. *, count(*) over(partition by id) as cnt
from t
) t
where cnt = 4;
如果數值可以超出這個范圍,可以使用條件聚合:
select t.*
from (select t.*,
count(case when value in(0, 1, 2, 3) then 1 else 0 end) over (partition by id) as cnt
from t
) t
where cnt = 4;
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321946.html
標籤:
上一篇:以下命令中的-proot是什么?
