如何分组、计算连续日期并将其用作 Netezza 中的过滤器

How to group, count consecutive dates and use it as filter in Netezza

我正在尝试对连续日期进行分组,对连续日期进行计数,并将该计数用作过滤器。

我有一个 table 目前看起来像:

pat_id   admin_dates   admin_grp    daily_admin    
-------------------------------------------------
1        08/20/2018        1          2 doses
1        08/21/2018        1          3 doses
1        08/22/2018        1          1 doses
1        10/05/2018        2          3 doses
1        12/10/2018        3          4 doses
2        01/05/2019        1          1 doses
2        02/10/2019        2          2 doses
2        02/11/2019        2          2 doses

其中 admin_grppat_id 对连续日期进行分组。

我想排除相同 pat_id 的连续日期少于 3 个的所有行。在这个例子中,只有 pat_id = 1 and admin_grp = 1 条件有 3 个连续的日期,我希望在结果中看到。我想要的输出是:

pat_id   admin_dates   admin_grp    daily_admin    
-------------------------------------------------
1        08/20/2018        1          2 doses
1        08/21/2018        1          3 doses
1        08/22/2018        1          1 doses

老实说,我没有知道如何执行此操作..我的尝试未能计算出有多少admin_grp在相同的pat_id中具有相同的值,让单独使用该计数作为过滤器。如果有人可以帮助/提出解决此问题的想法,我们将不胜感激。

简短回答:在“pat_id”上加入 table 并适当过滤:

Select a.* from TABLE a 
join (Select * from TABLE where daily_admin=‘3 doses’) b
using (pat_id) 
Where a.daily_admin in (‘1 doses’, ‘2 doses’, ‘3 doses’)

顺便说一句:太糟糕了,'daily_admin' 列不是整数...更好的数据模型会使 Where 语句稍微简单一些:)

假设任何 admin_grp 只会有连续的几天,您只需要按 (patid,admin_grp) 计算那些有 3 条或更多条记录的记录。

例如:

select x.* 
  from (select t.*
               ,count(*) over(partition by patid,admin_grp) as cnt
          from table t
       )x
 where x.cnt>=3