在日期、学生框架中按条件取最近的行

Take the nearest row by condition in date, student frame

我正在努力在(+/- 3 小时)内从 Test= 'Marked A+' 获得最近的 'Math Test' 或 'Biology Test',包括 TestOrder 排序。

如果 'Math Test' 或 'Biology Test' 同时在 'Marked A+' 之前 - 我得到 'Marked A+'

的最大 TestOrder

如果 'Math Test' 或 'Biology Test' 同时在 'Marked A+' 之后 - 我得到 'Marked A+'

的最小测试订单
| student | Test         | TestOrder | DateTime                |
| ------- | ------------ | --------- | ----------------------- |
| 1       | Math Test    | 22        | 2022-05-01 19:06:16.207 |
| 1       | Biology Test | 32        | 2022-05-01 19:06:16.207 |
| 1       | Marked A+    | 50        | 2022-05-01 19:06:16.407 |
| 1       | Math Test    | 22        | 2022-05-01 20:06:16.100 |
| 1       | Biology Test | 32        | 2022-05-01 20:06:16.100 |
| 2       | Math Test    | 22        | 2022-05-01 18:06:16.407 |
| 2       | Biology Test | 32        | 2022-05-01 18:06:16.407 |
| 2       | Marked A+    | 50        | 2022-05-01 19:06:16.407 |
| 2       | Math Test    | 22        | 2022-05-01 19:07:16.407 |
| 2       | Biology Test | 32        | 2022-05-01 19:07:16.407 |
| 3       | Math Test    | 22        | 2022-05-01 10:36:12.207 |
| 3       | Biology Test | 32        | 2022-05-01 19:02:16.407 |
| 3       | Marked A+    | 50        | 2022-05-01 19:06:16.407 |
| 3       | Math Test    | 22        | 2022-05-01 20:06:14.002 |
| 3       | Biology Test | 32        | 2022-05-01 21:06:10.107 |
| 4       | Math Test    | 22        | 2022-05-01 17:06:22.101 |
| 4       | Biology Test | 32        | 2022-05-01 18:06:22.101 |
| 4       | Marked A+    | 50        | 2022-05-01 19:06:16.407 |
| 4       | Math Test    | 22        | 2022-05-01 19:06:20.407 |
| 4       | Biology Test | 32        | 2022-05-01 23:06:20.407 |

学生的最终结果 'Marked A+' 加入了最近的事件 'Math Test' 或 'Biology Test'。

| student | Test      | TestOrder | DateTime                | student\_ | Test\_       | TestOrder\_ | DateTime\_              |
| ------- | --------- | --------- | ----------------------- | --------- | ------------ | ----------- | ----------------------- |
| 1       | Marked A+ | 50        | 2022-05-01 19:06:16.407 | 1         | Biology Test | 32          | 2022-05-01 19:06:16.207 |
| 2       | Marked A+ | 50        | 2022-05-01 19:06:16.407 | 2         | Math Test    | 22          | 2022-05-01 19:07:16.407 |
| 3       | Marked A+ | 50        | 2022-05-01 19:06:16.407 | 3         | Biology Test | 32          | 2022-05-01 19:02:16.407 |
| 4       | Marked A+ | 50        | 2022-05-01 19:06:16.407 | 4         | Math Test    | 22          | 2022-05-01 19:06:20.407 |
SELECT t1.student, t1.Test, t1.TestOrder, t1.Datetime
, t2.student_, t2.Test_, t2.TestOrder_, t2.Datetime_
FROM tab1 t1
left join tab1 t2 
    ON t2.Test in ('Math Test', 'Biology Test')          
    AND t2.student = t1.student
    AND DATEADD(HOUR, -3, t1.DateTime) <= t2.DateTime and t2.DateTime < DATEADD(HOUR, 3, t1.DateTime) /* Take from the nearest 'Marked A+' (+/-3 hours) */
WHERE t1.Test = 'Marked A+'

但我不知道如何根据 TestOrder 列

取最近的 'Math Test'、'Biology Test'

PS: 我正在使用 MSSQL

在我突出显示的屏幕截图中,必须采取行

以下使用 apply 匹配最接近 [Datetime] 的行到源行。但是它不处理重复项,您还没有澄清任何要求,但看看这是否适合您?

select *
from t
cross apply (
    select top(1) *
    from t t2
    where t.student = t2.student and t2.Test in ('Math Test', 'Biology Test') 
      and Abs(DateDiff(minute,t2.[datetime],t.[datetime]))<= 180
    order by Abs(DateDiff(minute,t2.[datetime],t.[datetime])),
     case when t2.[datetime] > t.[datetime] then TestOrder end ,
     case when t2.[datetime] < t.[datetime] then TestOrder end desc
)m
where t.test='Marked A+';

Demo Fiddle