繁体   English   中英

如何 select 在给定列中至少具有两个特定实例的 ID

[英]How to select IDs that have at least two specific instaces in a given column

我正在使用 pyspark 中的医疗索赔表,我只想返回至少有 2 个 claim_id 的用户 ID。 我的桌子看起来像这样:

claim_id |  userid |  diagnosis_type |  claim_type
__________________________________________________
1            1            C100            M
2            1            C100a           M
3            2            D50             F
5            3            G200            M
6            3            C100            M
7            4            C100a           M
8            4            D50             F
9            4            A25             F

在此示例中,我只想返回用户 ID 的 1、3 和 4。 目前我正在构建一个临时表来计算 claim_ids 的所有不同实例

create table temp.claim_count as
select distinct userid, count(distinct claim_id) as claims
from medical_claims
group by userid

然后当 claim_id 的数量 >1 时从该表中提取

select distinct userid
from medical_claims
where userid (
    select distinct userid
    from temp.claim_count
    where claims>1)

有没有更好/更有效的方法来做到这一点?

如果您只想要 id,请使用group by

select userid, count(*) as claims
from medical_claims
group by userid
having count(*) > 1;

如果您想要原始行,请使用 window 函数:

select mc.*
from (select mc.*, count(*) over (partition by userid) as num_claims
      from medical_claims mc
     ) mc
where num_claims > 1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM