[英]SQL MAX query using 3 tables
我在查詢 SQL 數據庫時遇到了困難。
| 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 |
| 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 |
| patient_id | episode_number | admit_date |
| ---------- | -------------- | ---------- |
| 111 | 4 | 01/05/2017 |
| 222 | 8 | 03/17/2017 |
| 222 | 9 | 03/20/2017 |
| 333 | 2 | 10/08/2017 |
我尋求一個 SQL 查詢,我可以在其中輸入一個 staff_id,然后讓它返回他們當前分配的所有開放劇集。 結果:
| staff_id | patient_id | episode_number | admit_date | date_of_assignment |
| -------- | ---------- | -------------- | ---------- | ------------------ |
| 4444 | 111 | 4 | 01/05/2017 | 01/05/2017 |
| 4444 | 222 | 8 | 03/17/2017 | 05/18/2017 |
我不明白 SQL 如何處理查詢中的別名。
The SQL doesn't know what to do with SQL window functions such as OVER
, LAG()
, LEAD()
, etc. So I'm using self joins along with the MAX()
function. 也許這是一個較舊的 SAP SQL 服務器。
我不知道 SQL 查詢中的大小寫是否無關緊要。
根據您的問題很難解決。 我想您需要為每個患者/病歷分配最新任務。
您可以使用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
調整查詢項后,這是返回正確結果的 SQL 查詢版本:
SELECT
t1.staff_id
, t1.patient_id
, t3.admit_date
, t1.episode_number
, t1.date_of_assignment
FROM
/* select the most recent attending practitioner entry from table1 for the patient and episode */
(SELECT attending_practitioner AS staff_id, patient_id, episode_number, pract_assignment_date AS date_of_assignment
FROM table1 AS t1a
WHERE t1a.pract_assignment_date =
(SELECT MAX(pract_assignment_date)
FROM table1 AS t1b
WHERE t1b.patient_id = t1a.patient_id
AND t1b.episode_number = t1a.episode_number)
UNION
/* select the most recent practitioner entry from table2 for the patient and episode */
SELECT backup_practitioner AS staff_id, patient_id, episode_number, date_of_assignment
FROM table2 AS t2a
WHERE t2a.date_of_assignment =
(SELECT MAX(date_of_assignment)
FROM table2 AS t2b
WHERE t2b.patient_id = t2a.patient_id
AND t2b.episode_number = t2a.episode_number)
) AS t1
INNER JOIN
/* filter out closed episodes by using the table3 */
(SELECT patient_id AS patient_id2, episode_number AS episode_number2, admit_date
FROM table3) AS t3
ON t3.patient_id = t1.patient_id
AND t3.episode_number2 = t1.episode_number
WHERE t1.staff_id = '4444'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.