在 12 个月的滚动期间(从当月回溯 11 个月)每个月每个医生的不同患者数

count of distinct patients per doctor per month for a rolling period of 12 month(11 months look back from the current month)

我有一个 table,带有 doctor_id、claim_date(日期类型)、claim_date_mo_id(字符类型)和 patient_id。 一位医生的示例数据集如下所示:

**DOCTOR_ID**   **CLAIM_DATE**  **CLAIM_DATE_MO_ID**    **PATIENT_ID**  
22222         7/29/2015       201507        12769998  
22222         9/29/2015       201509        12769998  
22222         9/7/2016        201609        756850  
22222         10/6/2016       201610        756850  
22222         4/11/2017       201704        837125  
22222         4/11/2017       201704        837125  
22222         4/11/2017       201704        837125  
22222         4/13/2017       201704        892834  
22222         5/15/2017       201705        837125  
22222         5/15/2017       201705        837125  
22222         5/15/2017       201705        837125  
22222         7/6/2017        201707        892834  
22222         9/6/2017        201709        17539987  
22222         9/19/2017       201709        837125  
22222         10/3/2017       201710        756850  
22222         10/3/2017       201710        756850  
22222         10/9/2017       201710        17539987  

输出应该如下:

**DOCTOR_ID**   **CLAIM_DATE_MO_ID**    **count(distinct patient)**  
22222             201507              1  
22222             201509              1  
22222             201609              1  
22222             201610              1  
22222             201704              3  
22222             201705              3  
22222             201707              3  
22222             201709              4  
22222             201710              4  

该计数是从当月到过去 11 个月期间医生接诊的不同患者的数量。 例如 201507 表示期间(201507-201408)。

注意:我必须取任何时期内不同患者数的最大值。
一种情况是 month_id 201704,其中过去 11 个月(不包括 201704 的当前月份)的不同患者计数为 2 到 4 月 11 日,但对于 4 月 13 日,不同患者的计数变为 3,这将被捕获.
所以对于 201704,计数应该是 3。

我尝试了以下查询,但没有得到所需的输出。

sel doctor_id,case when to_number(claim_date_mo_id)-lookback_12m is not null then claim_date_mo_id||lookback_12m end,count(distinct patient_id)  
from  
(  
sel doctor_id,patient_id,claim_date_mo_id ,
to_number(to_char(cast(trim(claim_date_mo_id)||'01' as date format'YYYYMMDD') - interval '11' month 'YYYYMM')) as lookback_12m  
from table  
where doctor_id=22222  
) A  
group by 1,2  

我不知道 Teradata,所以我会把它当作我最精通的 (Firebird) 来回答。怎么样

    select a.doctor_id,
           cast( cast( substring( a.claim_date_mo_id from 1 for 4 ) as integer ) - 1 as char( 4 ) ) || substring( a.claim_date_mo_id from 5 for 2 ) || '-' || a.claim_date_mo_id claim_period,
           count( distinct b.patient_id ) distinct_patients
    from table a
    join table b on a.doctor_id = b.doctor_id 
           and b.claim_date_mo_id > cast( cast( substring( a.claim_date_mo_id from 1 for 4 ) as integer ) - 1 as char( 4 ) ) || substring( a.claim_date_mo_id from 5 for 2 )
           and b.claim_date_mo_id <= a.claim_date_mo_id
    where a.doctor_id = 22222
    group by 1,2

这是一个替代方案:

    WITH MyCTE( DOCTOR_ID, CLAIM_DATE_MO_ID, LOOKBACK_12M ) AS
    ( SELECT DISTINCT DOCTOR_ID, CLAIM_DATE_MO_ID, 
                      to_number(to_char(cast(trim(claim_date_mo_id)||'01' as date format'YYYYMMDD') - interval '11' month 'YYYYMM')) 
      FROM MyTable 
      WHERE DOCTOR_ID = 22222 )
    SELECT CTE.DOCTOR_ID, CTE.CLAIM_DATE_MO_ID || CTE.LOOKBACK_12M, COUNT(DISTINCT T.PATIENT_ID
    FROM MyCTE CTE
    JOIN MyTable T ON CTE.DOCTOR_ID = T.DOCTOR_ID
                  AND T.CLAIM_DATE_MO_ID >= CTE.LOOKBACK_12M
                  AND T.CLAIM_DATE_MO_ID <= CTE.CLAIM_DATE_MO_ID 
    GROUP BY 1, 2

HTH, 集