如果類'Math'或'Biology'的標志為 NULL 或 = 0,我們需要與下面第一個出現(根據日期)的類'Literature'或'Englishliterature'左連接。
如果“數學”或“生物學”類的標志 = 1,那么我們需要與最新的(根據日期)“文學”或“英語文學”類左連接
初始表:
| class | date | flag |
| ------------------ | ----------------------- | ---- |
| Math | 2020-07-07 20:08:00.000 | 0 |
| Biology | 2020-07-07 21:08:00.000 | 1 |
| Math | 2020-07-08 17:08:00.000 | NULL |
| English | 2020-07-10 13:08:00.000 | 0 |
| Literature | 2020-07-15 20:08:00.000 | |
| English Literature | 2020-07-15 21:08:00.000 | |
| Math | 2020-07-15 22:08:00.000 | 1 |
| Literature | 2020-09-09 20:08:00.000 | |
| Math | 2020-09-16 11:08:00.000 | 1 |
| English | 2020-09-17 13:18:00.000 | 0 |
| Biology | 2020-09-19 13:18:00.000 | NULL |
結果表(包含無類='文學','英語文學'):
| class | date | flag | class_2 | date_2 |
| ------- | ----------------------- | ---- | ------------------ | ----------------------- |
| Math | 2020-07-07 20:08:00.000 | 0 | Literature | 2020-07-15 20:08:00.000 |
| Biology | 2020-07-07 21:08:00.000 | 1 | | |
| Math | 2020-07-08 17:08:00.000 | NULL | Literature | 2020-07-15 20:08:00.000 |
| English | 2020-07-10 13:08:00.000 | 0 | | |
| Math | 2020-07-15 22:08:00.000 | 1 | English Literature | 2020-07-15 21:08:00.000 |
| Math | 2020-09-16 11:08:00.000 | 1 | Literature | 2020-09-09 20:08:00.000 |
| English | 2020-09-17 13:18:00.000 | 0 | | |
| Biology | 2020-09-19 13:18:00.000 | NULL | | |
說明:
- 生物學在2020-09-19 13:18:00.000沒有連接,因為它的標志為 NULL且低于(晚于“生物學”)根據規則沒有帶有“文學”和“英語文學”的行。
- 生物學在2020-07-07 21:08:00.000與標志 1也沒有連接,因為之前沒有“英國文學”或“文學”
3) 2020-07-07 20:08:00.000 的數學,標志為 0(它是第一行)。由于標志為 0,所以我們看一下這個類之后第一個出現的“文學”或“英國文學”類。第一個出現是 2020-07-15 20:08:00.000 的“文學”,所以我們匹配它們
如何根據日期查找最近或第一行的 LEFT JOIN?
uj5u.com熱心網友回復:
沒有聲稱它是最優雅的解決方案;這會起作用:
with Tbl as (
select Class, Date=cast(date as datetime), flag
from (values
('Math',' 2020-07-07 20:08:00.000 ', 0 )
,('Biology',' 2020-07-07 21:08:00.000 ', 1 )
,('Math',' 2020-07-08 17:08:00.000 ', NULL )
,('English',' 2020-07-10 13:08:00.000 ', 0 )
,('Literature',' 2020-07-15 20:08:00.000 ',NULL )
,('English Literature',' 2020-07-15 21:08:00.000 ',NULL )
,('Math',' 2020-07-15 22:08:00.000 ', 1 )
,('Literature',' 2020-09-09 20:08:00.000 ',NULL )
,('Math',' 2020-09-16 11:08:00.000 ', 1 )
,('English',' 2020-09-17 13:18:00.000 ', 0 )
,('Biology',' 2020-09-19 13:18:00.000 ', NULL )
) T(Class, date, flag)
),
T as (
select
Tbl.*
, Case when Class in ('Math','Biology') then 'MB'
when Class in ('Literature','English Literature') then 'LE'
else 'Others'
end as ClassGroup
from Tbl
)
select *
from (select Ta.*
, Tb.Class as B_Class
, Tb.date as B_Date
, Tb.flag as B_Flag
, SeqAsc=row_number() over (partition by ta.ClassGroup, ta.Date order by tb.Date asc)
, SeqDesc=row_number() over (partition by ta.ClassGroup, ta.Date order by tb.Date desc)
from T as Ta
left join
T as Tb
on Ta.ClassGroup='MB'
and Tb.ClassGroup='LE'
and ((coalesce(Ta.flag,0)=0 and Ta.date <= Tb.date)
OR
(coalesce(Ta.flag,0)=1 and Ta.date >= Tb.date)
)
where Ta.ClassGroup in ('MB','Others')
) T
where
(coalesce(T.flag,0)=0 and SeqAsc=1)
or (coalesce(T.flag,0)=1 and SeqDesc=1)
or B_Class is null
order by Date, class
您有一些空白標志值,我假設為 NULL,您沒有指出您使用的資料庫產品,我假設為 SqlServer(但它應該適用于其中的大多數)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/459478.html
上一篇:選擇所有行值作為串列
