【问题标题】:difficulty conceptualizing sql query难以概念化 sql 查询
【发布时间】:2016-11-03 16:30:09
【问题描述】:

我对当前的报告问题感到有些困惑,希望能得到一些帮助。

一位医生即将退休。她的病人需要被告知她的退休。在过去两年中,构成“她的病人”的人比其他任何医生都看她的次数更多。如果做不到这一点(即在过去 2 年中分别看过她和另一位医生 2 次),如果他们最近去看她,他们就是“她的病人”。

这是我目前所拥有的。

select patient_id, last_name, first_name, post_fromdate, performing_md_id, performing_md_name, category_desc, procedure_code
from [table]
where post_fromdate >= ADD_MONTHS(SYSDATE,-24)
and category_desc like '%WELL%' --specific visit type
and patient id in ([list of patients])
group by patient_id, last_name, first_name, post_fromdate, performing_md_id, performing_md_name, category_desc, procedure_code
order by patient_id, post_fromdate desc

回顾一下……

我所拥有的:每位医生就诊的每位患者的清单,包括对其他医生的就诊。

我所寻求的:一些额外的标准将表明退休医生是否被每位患者访问的次数最多。如果与给定患者的其他医生看到的相同但不少于其他医生,那么如果退休医生最近就诊。

我们欣然接受任何帮助。提前谢谢你。

编辑:将输出所需的结果,为我提供 select 子句中的信息,每个 Patient_id 有一个且只有一个 perform_md_id 表示该患者的医生(他们在过去两年中看过最多次数的医生。如果不是那个,然后是他们最近看过的医生)。

【问题讨论】:

  • 发布您的架构、一些示例数据和所需的结果。
  • 您需要在查询中有 count(visits) - 发布相关表的架构。
  • 这就是灾难:这一切都在一张桌子上。这个数据库系统一直是创可贴的解决方案。即使是这里的生活者也不知道任何信息的位置。
  • 我不明白编辑...我以为退休的医生是众所周知的,你需要知道所有让他作为“主要”医生的病人。 EDIT 说的是别的东西——你想为所有病人确定主治医生。这与您最初的问题有什么关系? (或者你认为这是解决你原来问题的唯一方法吗?)

标签: sql oracle selection


【解决方案1】:

类似的东西。表名和列名是组成的,但应该是不言自明的。

select patient_id, 
       max(doctor_id) keep (dense_rank last order by ct, last_visit) as primary_doctor_id
from   (
         select   patient_id, doctor_id, count(*) as ct, max(visit_date) as last_visit
         from     visits
         group by patient_id, doctor_id
       )
group by patient_id
;

【讨论】:

    【解决方案2】:

    我假设你有一张表格,形状是病人、医生、就诊日期

    select patient,[This Doc] as [This DOC Visit Count], [Other Doc] as [Other Doc Visit Count]
    into #visit_counts
    from (select patient, case when doctor = 'retiring doc' then 'This Doc' else 'Other Doc' end as Doc, visit_date from doc_patient_visit_table ) P
    Pivot (count(visit_date) for Doc in ([This Doc],[Other Doc])) Pvt
    
    select patient,[This Doc] as [This DOC Last Visit], [Other Doc] as [Other Doc Last visit]
    into #visit_times
    from (select patient, case when doctor = 'retiring doc' then 'This Doc' else 'Other Doc' end as Doc, visit_date from doc_patient_visit_table) P
    Pivot (max(visit_date) for Doc in ([This Doc],[Other Doc])) Pvt
    
    select patient, [This DOC Visit Count],  [Other Doc Visit Count], [This DOC Last Visit], [Other Doc Last visit]
    from #visit_counts t1
    join #visit_times t2 on t1.patient = t2.patient
    where [This DOC Visit Count] > [Other Doc Visit Count] or ([This DOC Visit Count] = [Other Doc Visit Count] and [This DOC Last Visit] > [Other Doc Last visit])
    

    另一种方法

    Select patient, 
    sum(case when doc = 'retiring_doc' then 1 else 0 end) as [This DOC Visit Count],
    max(case when doc = 'retiring_doc' then visit_date end)  as [This DOC Last Visit],
    sum(case when doc != 'retiring_doc' then 1 else 0 end) as [Other Doc Visit Count],
    max(case when doc != 'retiring_doc' then visit_date end)  as [Other Doc Last visit]
    from from doc_patient_visit_table
    group by patient
    having sum(case when doc = 'retiring_doc' then 1 else 0 end) > sum(case when doc != 'retiring_doc' then 1 else 0 end) 
    OR (sum(case when doc = 'retiring_doc' then 1 else 0 end) = sum(case when doc != 'retiring_doc' then 1 else 0 end)
    AND [This DOC Last Visit] > [Other Doc Last visit])
    

    【讨论】:

    • 这完全有道理。我会给它一个ol'大学尝试。感谢您的意见!
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2012-07-02
    • 2015-10-09
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多