[英]Find customers who switched where they received medical care using SQL
I wish to identify those customers who MOST RECENTLY switched where they received their medical care over a two year period.我想确定那些最近在两年内更换了接受医疗服务的客户。 I would like to find the last two transactions per customer over the two year period where the diagnosis code is the same but site of service is different .我想找到两年内每个客户的最后两笔交易,其中诊断代码相同但服务站点不同。
For example, member 1 has visited both their physician's office and the outpatient facility many times in the two year period, but I am only interested in the last two transactions where they have switched site of service for the same diagnosis code.例如,成员 1 在两年内多次访问他们的医生办公室和门诊设施,但我只对最后两次他们为相同诊断代码切换服务站点的交易感兴趣。
As an example my data is structured as followed:例如,我的数据结构如下:
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
Any help with solving this would be greatly appreciated.解决此问题的任何帮助将不胜感激。
I am using Sybase IQ.我正在使用 Sybase IQ。
You can use:您可以使用:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.