按日期,学生框架按最近的行
我正在努力从test ='标记为A+'(包括测试订单)的(+/- 3小时)中获得最近的“数学测试”或“生物学测试”。
如果“数学测试”或“生物学测试”是在同一时间标记为A+'之前的 - 我将最大测试仪与“标记为A+”
if“ Math Test”或“ Biology Test”进行了同一时间 - 我将Min Testorder到“标记为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 |
最终结果已标记为A+“左将连接到最近的活动“数学测试”或“生物学测试”。
| 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列
PS进行最近的“数学测试”,“生物学测试”:我在屏幕截图上使用MSSQL
I am struggling to get the nearest 'Math Test' or 'Biology Test' in (+/- 3 hours) from Test= 'Marked A+' including TestOrder ordering.
If 'Math Test' or 'Biology Test' were before 'Marked A+' with the same time - I get the max TestOrder to the 'Marked A+'
If 'Math Test' or 'Biology Test' were after 'Marked A+' with the same time - I get the min TestOrder to the '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 |
Final result has 'Marked A+' left joined to the nearest events 'Math Test' or 'Biology Test' across students.
| 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+'
But I have no I idea how to take the nearest 'Math Test', 'Biology Test' according to TestOrder column
PS: I am using MSSQL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下使用
应用
将[DateTime]
与源行的最接近行匹配。但是,它并不涉及重复项,您尚未澄清任何要求,但是看看这是否对您有用?参见
The following uses
apply
to match the closest row by[Datetime]
to the source row. It doesn't deal with duplicates however, for which you haven't clarified any requirements, but see if this works for you?See Demo Fiddle