按日期,学生框架按最近的行

发布于 2025-01-26 18:05:48 字数 3125 浏览 2 评论 0原文

我正在努力从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

On the screenshot I highlighted must be taken rows enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2025-02-02 18:05:48

以下使用应用[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+';

参见

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?

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+';

See Demo Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文