使用 SQL 查找更换接受医疗服务地点的客户

Find customers who switched where they received medical care using SQL

我想确定那些最近 在两年内更换了接受医疗服务的客户。我想找到每个客户在两年内 诊断代码相同 服务站点不同 的最后两笔交易。

例如,会员 1 在两年内多次访问他们的医生办公室和门诊设施,但我只对最后两次他们为相同诊断代码更换服务地点的交易感兴趣.

例如,我的数据结构如下:

MEMBER_ID DIAGNOSIS_CD  DATE        SITE_OF_SERVICE
1         A             April       Physician
1         A             May         Physician
2         A             May         Home Infusion
1         B             May         Physician
1         A             July        Outpatient
1         A             August      Physician
1         A             September   Physician
1         A             October     Outpatient

如能帮助解决此问题,我们将不胜感激。

我正在使用 Sybase IQ。

您可以使用:

select
  member_id
from (
  select
    *,
    row_number() 
      over (partition by member_id, diagnosis_cd order by date desc) as rn
  from t
) x
where rn <= 2
group by member_id
having min(site_of_service) <> max(site_of_service)