我有 2 張桌子,TABLE1:
job job_type job_id start_time end_time
hello EXPRESS 125445801 6/9/22 10:59 6/9/22 11:59
hello EXPRESS 125449030 6/9/22 11:19 6/9/22 11:38
hello EXPRESS 125444620 6/9/22 10:59 6/9/22 11:11
和表2:
job time_sample
hello 6/9/22 10:59
hello 6/9/22 11:13
hello 6/9/22 11:21
hello 6/9/22 11:49
問題在第一張桌子上。由于開始和結束時間的重疊,我需要能夠區分是否有重疊。如果有重疊,那么我需要將類別標識為“MULTIPLE”。如果沒有重疊,那么我只將類別標記為。作業型別。加入第二個表時,第二個表需要填充 job_type,以便如果 TABLE2 中的 time_sample 僅介于 TABLE1 中僅 1 條記錄的開始時間和結束時間之間,它將顯示一個 job_type。如果 time_sample 落在該一項作業的 1 個以上的開始/結束時間內,那么它應該填充 MULTIPLE。我很難理解如何能夠執行這種半聚合/連接邏輯來創建以下內容:
job time_sample job_type
hello 6/9/22 10:59 MULTIPLE
hello 6/9/22 11:13 EXPRESS
hello 6/9/22 11:21 MULTIPLE
hello 6/9/22 11:49 EXPRESS
連接應該在“作業”上,并且 TABLE2.time_sample 在 TABLE1.start_time 和 TABLE1.end_time 之間
uj5u.com熱心網友回復:
您可以在表之間進行簡單的連接。對于作業型別,檢查是否有多個匹配項:
select
t2.job,
t2.time_sample,
if (count(*)>1, 'MULTIPLE', max(t1.job_type)) as "job_type"
from table2 t2
join table1 t1 on t2.time_sample between t1.start_time and t1.end_time
group by t2.job, t2.time_sample
見分貝小提琴
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/491958.html
上一篇:即使在SELECT查詢中滿足加入條件后也獲得額外的e_id
下一篇:合并1列行相同的資料
