使用4个表的SQL MAX查询

时间:2018-10-30 01:13:25

标签: sql

我很难从SQL数据库获取信息。

我正在使用的表如下所示:


    Table: system.episode_history
    +------------+----------------+------------+----------------+
    | patient_id | episode_number | admit_date | discharge_date |
    +------------+----------------+------------+----------------+
    | 111        | 4              | 01/05/2017 |                | 
    +------------+----------------+------------+----------------+
    | 222        | 8              | 03/17/2017 |                | 
    +------------+----------------+------------+----------------+
    | 222        | 9              | 03/20/2017 |                | 
    +------------+----------------+------------+----------------+
    | 333        | 2              | 10/08/2017 |                |
    +------------+----------------+------------+----------------+
    | 444        | 7              | 08/09/2017 | 08/20/2017     |
    +------------+----------------+------------+----------------+

    Table: system.view_episode_summary_current
    +------------+----------------+------------+----------------------+
    | patient_id | episode_number | admit_date | last_date_of_service |
    +------------+----------------+------------+----------------------+
    | 111        | 4              | 01/05/2017 |  11/01/2017          | 
    +------------+----------------+------------+----------------------+
    | 222        | 8              | 03/17/2017 |  11/03/2017          | 
    +------------+----------------+------------+----------------------+
    | 222        | 9              | 03/20/2017 |  11/04/2017          | 
    +------------+----------------+------------+----------------------+
    | 333        | 2              | 10/08/2017 |  11/05/2017          |
    +------------+----------------+------------+----------------------+

    Table: system.history_attending_practitioner
    +------------+----------------+---------------------+-----------------------+
    | patient_id | episode_number | attend_practitioner | pract_assignment_date |
    +------------+----------------+---------------------+-----------------------+
    | 111        | 4              | 4444                | 01/05/2017            |
    +------------+----------------+---------------------+-----------------------+
    | 222        | 8              | 5555                | 03/17/2017            |
    +------------+----------------+---------------------+-----------------------+
    | 222        | 8              | 6666                | 03/20/2017            |
    +------------+----------------+---------------------+-----------------------+
    | 222        | 9              | 7777                | 04/10/2017            |
    +------------+----------------+---------------------+-----------------------+
    | 333        | 2              | 5555                | 10/08/2017            |
    +------------+----------------+---------------------+-----------------------+
    | 444        | 7              | 7777                | 08/09/2017            |
    +------------+----------------+---------------------+-----------------------+

    Table: system.user_practitioner_assignment
    +------------+----------------+---------------------+--------------------+
    | patient_id | episode_number | backup_practitioner | date_of_assignment |
    +------------+----------------+---------------------+--------------------+
    | 111        | 4              |                     |                    |
    +------------+----------------+---------------------+--------------------+
    | 222        | 8              | 7777                | 03/17/2017         |
    +------------+----------------+---------------------+--------------------+
    | 222        | 8              | 4444                | 05/18/2017         |
    +------------+----------------+---------------------+--------------------+
    | 222        | 9              |                     |                    |
    +------------+----------------+---------------------+--------------------+
    | 333        | 2              | 4444                | 10/08/2017         |
    +------------+----------------+---------------------+--------------------+
    | 333        | 2              | 5555                | 10/19/2017         |
    +------------+----------------+---------------------+--------------------+

我需要SQL查询来返回这样的表:


    +-------------+------------+----------------+------------+--------------------+----------------------+
    | {?Staff_ID} | patient_id | episode_number | admit_date | date_of_assignment | last_date_of_service |
    +-------------+------------+----------------+------------+--------------------+----------------------+
    | 4444        | 111        | 4              | 01/05/2017 | 01/05/2017         |  11/01/2017          |
    +-------------+------------+----------------+------------+--------------------+----------------------+
    | 4444        | 222        | 8              | 03/17/2017 | 05/18/2017         |  11/03/2017          |
    +-------------+------------+----------------+------------+--------------------+----------------------+

我正在使用system.episode_history表来确定该剧集是否有出院日期。

位置(system.episode_history.discharge_date为NULL)

我正在使用system.view_episode_summary_current表获取与剧集相关的服务的最后日期。

我尝试了几件事,但没有成功。

首先,SQL处理程序似乎对查询中的别名无所适从。我还不知道如何安抚它。

第二,SQL句柄似乎已经足够老了,以至于它不知道如何处理OVER,LAG(),LEAD()等SQL窗口函数。因此,我只想弄清楚如何与MAX()函数一起使用自联接。我认为这是一台较旧的SAP SQL服务器。

我是SQL的新手。例如,根据我观察到的情况,我假设SQL查询中的大写字母(大部分)是不相关的,但我不确定。我一直在互联网上寻求帮助,但没有发现任何可以使我工作或理解的有用信息(在这一点上,大多数感觉像是外语)。我在工作内部和外部都知道SQL的人很困惑,但这是事实。我不在这里,请帮忙。

更新1

我对下面的建议查询进行了调整,直到没有错误为止,并最终得到了如下查询:

select staff.staff_id, epi.patient_id, epi.episode_number as episode_id, epi.admit_date, staff.date_of_assignment, curr.last_date_of_service
from system.episode_history as epi
join (
    select patient_id, episode_number, attend_practitioner as staff_id, pract_assignment_date as date_of_assignment 
    from system.history_attending_practitioner
    union
    select patient_id, episode_number, backup_practitioner as staff_id, date_of_assignment as date_of_assignment 
    from system.user_practitioner_assignment
    where (backup_practitioner is not null)
    ) as staff on staff.patient_id=epi.patient_id and staff.episode_number=epi.episode_number
    and not exists(select * from system.history_attending_practitioner as a2 where a2.patient_id=staff.patient_id and a2.episode_number=staff.episode_number and a2.pract_assignment_date>staff.date_of_assignment)
    and not exists(select * from system.user_practitioner_assignment as a3 where a3.patient_id=staff.patient_id and a3.episode_number=staff.episode_number and a3.date_of_assignment>staff.date_of_assignment)
join system.view_episode_summary_current curr on curr.patient_id=epi.patient_id and curr.episode_number=e.episode_number
where (epi.discharge_date is null)
and staff.staff_id = '4444'

当我对真实数据库运行此查询时,它正确地从system.history_attending_practitioner返回4个匹配项,但仅从system.user_practitioner_assignment返回8个匹配项中的1个。充其量,我以前的许多尝试只返回了一个表或另一个表的结果,但从未同时返回。因此,至少这是一个改进。

更新2

调整查询项目的顺序后,这是返回正确结果的版本:

select staff.staff_id, epi.patient_id, epi.episode_number as episode_id, epi.admit_date, staff.date_of_assignment, curr.last_date_of_service
from system.episode_history as epi
join (
    select patient_id, episode_number, attend_practitioner as staff_id, pract_assignment_date as date_of_assignment 
    from system.history_attending_practitioner
    where (attend_practitioner is not null)
    and not exists(select * from system.history_attending_practitioner as a2 where a2.patient_id=system.history_attending_practitioner.patient_id and a2.episode_number=system.history_attending_practitioner.episode_number and a2.pract_assignment_date>system.history_attending_practitioner.pract_assignment_date)
    union
    select patient_id, episode_number, backup_practitioner as staff_id, date_of_assignment as date_of_assignment 
    from system.user_practitioner_assignment
    where (backup_practitioner is not null)
    and not exists(select * from system.user_practitioner_assignment as a3 where a3.patient_id=system.user_practitioner_assignment.patient_id and a3.episode_number=system.user_practitioner_assignment.episode_number and a3.date_of_assignment>system.user_practitioner_assignment.date_of_assignment)
    ) as staff on staff.patient_id=epi.patient_id and staff.episode_number=epi.episode_number
join system.view_episode_summary_current curr on curr.patient_id=epi.patient_id and curr.episode_number=e.episode_number
where (epi.discharge_date is null)
and staff.staff_id = '4444'

1 个答案:

答案 0 :(得分:0)

根据您的问题很难解决。我想您需要为每个患者/病历分配最新任务。

您可以使用max使用第二个子查询来执行此操作,也可以按照以下方式使用不存在。

正如您所提到的,使用row_number()和/或CTE会容易得多,但这是批量标准sql版本。

select s.staff_id, e.patient_id, e.episode_number as episode_id, e.admit_date, s.date_of_assignment, c.last_date_of_service
from episode_history e
join (
    select patient_id, episode_number, attend_practitioner as staff_id, pract_assignment_date as date_of_assignment 
    from history_attending_practitioner
    union
    select patient_id, episode_number, backup_practitioner as staff_id, date_of_assignment as date_of_assignment 
    from user_practitioner_assignment
    where backup_practitioner is not null
    ) s on s.patient_id=e.patient_id and s.episode_number=e.episode_number
    and not exists(select * from history_attending_practitioner a2 where a2.patient_id=s.patient_id and a2.episode_number=s.episode_number and a2.pract_assignment_date>s.date_of_assignment)
    and not exists(select * from user_practitioner_assignment a3 where a3.patient_id=s.patient_id and a3.episode_number=s.episode_number and a3.date_of_assignment>s.date_of_assignment)
join view_episode_summary_current c on c.patient_id=e.patient_id and c.episode_number=e.episode_number
where e.discharge_date is null