我有以下数据:
rpart_model <- rpart(...)
environment(rpart_model$terms) <- NULL
对于每组记录(按ParentID分组),我想查找与DataValue中所有给定值匹配的所有组,例如:
我查看this question,这非常相似,但OP会查找不包含单个值的组。
非常感谢任何帮助!
答案 0 :(得分:2)
您可以使用条件聚合执行此操作:
select parentid
from tablename
group by parentid
having sum(case when datavalue = 1 then 1 else 0 end) > 0 and
sum(case when datavalue = 6 then 1 else 0 end) > 0
另一种方法是使用exists
:
select distinct parentid
from tablename t1
where exists(select * from tablename where parentid = t1.parentid and datavalue = 1) and
exists(select * from tablename where parentid = t1.parentid and datavalue = 6)
另一种方法是计算不同的事件:
select parentid
from tablename
where datavalue in(1, 6)
group by parentid
having count(distinct datavalue) = 2