在這個 SQL 中,我們從 use_log_with_transactions_view 獲取所有具有登錄歷史的員工)。但我需要讓所有員工即使他們沒有登錄歷史記錄。登錄歷史記錄在 use_log_with_transactions_view 中。我已經嘗試過外部、完整、右連接,但它們沒有連接到 = 部分(在 ul.staff_id = sv.staff_id 上)。有什么辦法可以讓從未登錄系統的員工?
select max(ul.session_start) as session_start, max(ul.session_end) as session_end, sv.last_name,
sv.first_name,
max(wr.description) as description
from staff_view sv
full join use_log_with_transactions_view ul
on ul.staff_id = sv.staff_id
inner join staff_worker_role_link_view sw
on ul.staff_id = sw.staff_id
inner join worker_role wr
on wr.worker_role_id = sw.worker_role_id
where wr.description in ('Program Leader/Supervisor',
'Program Administration', 'Licensed Clinician', 'Unlicensed Clinician','Service Provider')
and sv.first_name <> 'NTST' and sv.is_administrator = '0' and sv.end_date is null
group by sv.last_name, sv.first_name
order by sv.last_name, sv.first_name
uj5u.com熱心網友回復:
當您希望 SQL 查找不存在的內容時?
然后一種方法是將所有應該存在的東西與存在的東西相匹配。
有時這涉及交叉連接。
但是在這里,您可以將日志中的作業人員加入作業人員。
日志中缺失的內容與 staff_id 不匹配。
select staff.last_name, staff.first_name
, max(uselog.session_start) as session_start
, max(uselog.session_end) as session_end
, max(wrole.description) as description
from staff_view as staff
join staff_worker_role_link_view as staffrole
on staffrole.staff_id = staff.staff_id
join worker_role as wrole
on wrole.worker_role_id = staffrole.worker_role_id
left join (
select staff_id
, max(session_start) as session_start
, max(session_end) as session_end
from use_log_with_transactions_view
group by staff_id
) uselog
on uselog.staff_id = staff.staff_id
where wrole.description in ('Program Leader/Supervisor', 'Program Administration', 'Licensed Clinician', 'Unlicensed Clinician', 'Service Provider')
and staff.first_name <> 'NTST'
and staff.is_administrator = '0'
and staff.end_date is null
-- AND uselog.staff_id IS NULL -- uncomment for only those without logs
group by staff.last_name, staff.first_name
order by staff.last_name, staff.first_name
uj5u.com熱心網友回復:
如果你想要所有員工而不考慮登錄,你只需要做一個 LEFT JOIN,意思是,我想要 LEFT 表中的所有記錄(第一個列出),而不管在右表中找到的記錄(第二個列出)。
清理了可讀性,但現在您可以直觀地看到 SV 如何到達 UL 的層次結構,注意左連接。所以你總是得到 SV 記錄,但只有那些基于與 SV 關聯的 WHERE 條件的記錄。
請注意,我將 WR.Description 移到了相應的 JOIN 部分。這樣,它不會將左右系結為 INNER 連接,因為它不是 SV 的整體 WHERE 的一部分。它僅適用于它之前的 SW 的連接組件。
綜上所述,對于那些通過但沒有 UL(登錄)的記錄,它們的會話開始/結束和作業人員描述將具有 NULL 值。您可以將 coalesce() 應用于適合您的種子。
select
sv.last_name,
sv.first_name,
max(ul.session_start) as session_start,
max(ul.session_end) as session_end,
max(wr.description) as description
from
staff_view sv
LEFT join use_log_with_transactions_view ul
on sv.staff_id = ul.staff_id
inner join staff_worker_role_link_view sw
on ul.staff_id = sw.staff_id
inner join worker_role wr
on sw.worker_role_id = wr.worker_role_id
AND wr.description in ( 'Program Leader/Supervisor',
'Program Administration',
'Licensed Clinician',
'Unlicensed Clinician',
'Service Provider')
where
sv.first_name <> 'NTST'
and sv.is_administrator = '0'
and sv.end_date is null
group by
sv.last_name,
sv.first_name
order by
sv.last_name,
sv.first_name
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/413600.html
標籤:
上一篇:檢查SQL中的多個可互換列值
下一篇:資料庫設計問題
