检测超出日期间隔的重复项

时间:2011-08-24 20:21:44

标签: sql database oracle lag oracle11gr2

我搜索过但却无法找到答案。

有患者,医院,医疗分支(急诊室,泌尿科,整形外科,内科疾病等),医疗操作规范(检查,外科手术,核磁共振成像,超声或其他)和患者就诊日期。

病人去看医生,医生开药并要求再次进行控制检查。 如果患者在 10天后返回,则必须向同一家医院支付另一笔检查费。医院可以在10天后指定一个日期,告知在接下来的10天内没有可用的插槽,以便获得考试费用。

表结构如下:

Patient id.no   Hospital    Medical Branch     Medical Op. Code      Date
  1                 H1             M0                P1            01/05/2011
  5                 H1             M1                P9            03/05/2011
  3                 H2             M0                P2            09/05/2011
  1                 H1             M0                P1            14/05/2011 
  3                 H1             M0                P2            20/05/2011
  5                 H1             M2                P9            25/05/2011
  1                 H1             M0                P3            26/05/2011

在这里,访问患者没有。患者编号3和5不构成问题。 3访问不同的医院和患者5号访问不同的医疗分支。即使他们在10天内访问,他们也会支付考试费用。

然而,1号病人在同一个医院,同一个分院就诊,并在01/05和14/05进行相同的程序(P1:检查)。

26/05并不算数,因为它不是体检。

我要标记的是同一患者,同一医院,同一分支和相同的医疗操作代码(具体为体检:P1),日期范围超过10天。

结果表的格式:

HOSPITAL        TOTAL NUM. of PATIENTS      NUM. of PATIENTS OUT OF DATE RANGE
  H1                        x                              a
  H2                        y                              b
  H3                        z                              c

感谢。

3 个答案:

答案 0 :(得分:2)

再一次,它是拯救的分析功能。

此查询使用LAG()函数将YOUR_TABLE中的记录与表中的前一个(由DATE定义)匹配记录(由PATIENT_ID定义)链接。

select hospital_id
       , count(*) as total_num_of_patients      
       , sum (out_of_range) as num_of_patients_out_of_range
from (
    select patient_id
           , hospital_id
           , case
                   when hospital_id_1 = hospital_id_0
                   and  visit_1 > visit_0 + 10
                   and  med_op_code_1 = med_op_code_0
                   then 1
                   else 0
              end as out_of_range
    from (
            select patient_id
                   , hospital_id as hospital_id_1
                   , date as visit_1
                   , med_op_code as med_op_code_1
                   , lag (date) over (partition by patient_id order by date) as visit_0
                   , lag (hopital_id) over (partition by patient_id order by date) as hopital_id_0
                   , lag (med_op_code) over (partition by patient_id order by date) as med_op_code_0
            from your_table
            where med_op_code = 'P1'
        )
    )
group by hospital_id
/

警告:我没有测试过这段代码,因此它可能包含语法错误。我将在下次访问Oracle数据库时检查它。

答案 1 :(得分:1)

这有点粗糙,因为我还没有掌握Oracle DB,但关键特性是相同的:分析函数LAG()。除了它的伴侣功能LEAD()之外,它们还非常适合帮助处理诸如活动期间的事情。

这是我对代码的尝试:

select n.hospital, COUNT(n.patient_id) as patients_out_of_date_range
from (
    select *
    from (
        select d.*, lag(date, 1) over (partition by d.patient_id, d.hospital, d.medical_branch, d.medical_op_code order by d.date) as prev_date
        from datatable d inner join
            (
                select d.patient_id, d.hospital, d.medical_branch, d.medical_op_code
                from datatable d
                where d.medical_op_code = 'P1'
                group by d.patient_id, d.hospital, d.medical_branch, d.medical_op_code
                having COUNT(d.date) > 1
            ) t on d.patient_id  = t.patient_id and d.hospital = t.hospital and d.medical_branch = t.medical_branch and d.medical_op_code = t.medical_op_code
        ) m
    where date - prev_date > 10
    ) n
group by n.hospital

就像我说的,这没有经过测试,但至少应该让你开始朝着正确的方向前进。

一些参考文献: http://www.adp-gmbh.ch/ora/sql/analytical/lag.html

http://www.oracle-base.com/articles/misc/LagLeadAnalyticFunctions.php

答案 2 :(得分:1)

我认为这就是你想要的:

WITH Patient_Visits (Patient_Id, Hospital_Id, Branch_Id, Visit_Date, Visit_Order) as ( 
                     SELECT Patient_Id, Hospital_Id, BranchId, Visit_Date, 
                            ROW_NUMBER() OVER(PARTITION BY Patient_ID, Hospital_Id, Branch_Id, 
                                              ORDER_BY Patient_Id, Hospital_Id, Branch_Id, Visit_Date)
                    FROM Hospital_Visits
                    WHERE Procedure_Id = 'P1'),
     Hospital_Recent_Visits (Hospital_Id, Recent_Visitor_Count) as (
                             SELECT a.Hospital_Id, COUNT(DISTINCT a.Patient_Id)
                             FROM Patient_Visits as a
                             JOIN Patient_Visits as b
                               ON b.Hospital_Id = a.Hospital_Id
                                  AND b.Branch_Id = a.Branch_Id
                                  AND b.Patient_Id = a.Patient_Id
                                  AND b.Visit_Order = a.Visit_Order - 1
                                  AND b.Visit_Date + 10 > a.Visit_Date
                             GROUP BY a.Hospital_Id, a.Patient_Id),
     Hospital_Patient_Count (Hospital_Id, Patient_Count) as (
                             SELECT Hospital_Id, COUNT(DISTINCT Patient_Id)
                             FROM Hospital_Visits
                             GROUP BY Hospital_Id, Patient_Id)
SELECT a.Hospital_Id, b.Patient_Count, c.Recent_Visitor_Count
FROM Hospitals as a
LEFT JOIN Hospital_Patient_Count as b
ON b.Hospital_Id = a.Hospital_Id
LEFT JOIN Hospital_Recent_Visits as c
ON c.Hospital_id = a.Hospital_Id

请注意,这是针对DB2系统编写和测试的。我认为Oracle数据库具有相关的功能,因此查询仍然应该按照书面形式工作。但是,DB2似乎缺少Oracle的一些OLAP功能(至少我的版本),这可能对淘汰一些CTE很有用。