Impala: count(distinct) 有多个 where 语句条件?

Impala: count(distinct) with multiple where statement criteria?

我有一个健康结果数据库,我希望能够在其中查询在特定日期范围内满足特定诊断字符串的受试者的计数。虚拟数据 (my_table) 看起来像这样(Date 作为时间戳):

subjid   Diagnosis  Date
----------------------------------------
Subj001  Z12345     2019-02-05 00:00:00
Subj001  Z12345     2017-01-10 00:00:00
Subj002  Z12345     2018-08-14 00:00:00
Subj002  Z12345     2014-03-20 00:00:00
Subj002  Z12345     2013-07-23 00:00:00
Subj003  Y56789     2016-08-16 00:00:00

subjid 中的每个主题可以有多个条目,每个条目都有一个相应的诊断代码。到目前为止,我的查询看起来像:

select 
    subjid, Diagnosis, Date,
    count(subjid) over (partition by Diagnosis) as count 
from 
    my_table
where 
    Diagnosis in ('Z12345') 
    and diag_date >= '2014-01-01 00:00:00'

但是,问题是我不能在括号中包含不同的语句以进行计数,因为这 returns 是一个错误。我正在寻找的是在特定日期之后满足诊断代码字符串的唯一主题的数量;我对每位患者的诊断代码字符串出现多少次感兴趣。

问题:有没有办法计算每个给定对象在特定日期后与特定诊断字符串匹配的唯一出现总数?

解决方案需要在 Impala 中。提前感谢您的任何建议。

一种方法是dense_rank()s的总和:

select subjid, Diagnosis, Date,
       (dense_rank() over (partition by diagnosis order by subjid asc) +
        dense_rank() over (partition by diagnosis order by subjid desc)
       ) as num_subjids
from my_table
where Diagnosis in ('Z12345') and
      diag_date >= '2014-01-01 00:00:00';