SQL在时间戳之间加入

发布于 2025-02-13 00:27:02 字数 2262 浏览 0 评论 0原文

我有两个这样的桌子:

table1

时间戳
2022-07-04 16:16:50120 120
2022-07-07-04 16:17:25110
2022-07-07-07-04 16:17:35 105105 105
2022-07-04 16:17:45130

table2

begin_timestampend_timestamp
2022-07-04 16:14:002022-07-04 16:17:02
2022-07-04 16:17:17:112022-07-07-04 16:17:17:30
2022-07-04 16:17:382022-07-04 16:17:47

我想根据他们的时间戳加入两个桌子。因此,如果table1.timestamp位于table2.begin_timestamp和table2.end_timestamp之间,它将添加“ ISFOUND = 1”。例如,表1的第一行是16:16:50,在16:14:00和16:17:02(Table2的第一行)之间。我想要这样的桌子:

想要的表

时间戳ISFOUND
2022-07-04 16:16:50120 1201
2022-07-04 16:17:251101
2022-07-07-04 16: 17:351050
2022-07-04 16:17:451301

我尝试了以下内容,但仅适用于最后一行。

SELECT t1.timestamp, 
       t1.value,
       (CASE WHEN (t1.Timestamp BETWEEN t2.Begin_Timestamp AND t2.End_Timestamp)
             THEN '1'
        ELSE '0') AS isFound
FROM Table1 t1
LEFT JOIN (
   SELECT * 
   FROM Table2
) AS t2
ON t1.Timestamp BETWEEN t2.Begin_Timestamp AND t2.End_Timestamp 
)

结果:

时间戳ISFOUND
2022-07-04 16:16:50120NAULL
2022-07-04 16:17:25110NULL
2022-07-07-04 16:17:17:17:17:17:17: 35 105NAULL
20222-07-07-04 16:17: 451301

我使用MS-SQL Server。有人可以帮我吗?我觉得这很容易,但我没有发现问题。谢谢!

I have two tables like these:

Table1

Timestampvalue
2022-07-04 16:16:50120
2022-07-04 16:17:25110
2022-07-04 16:17:35105
2022-07-04 16:17:45130

Table2

Begin_TimestampEnd_Timestamp
2022-07-04 16:14:002022-07-04 16:17:02
2022-07-04 16:17:112022-07-04 16:17:30
2022-07-04 16:17:382022-07-04 16:17:47

I want to join the two tables based on their timestamp. So if the table1.timestamp is between table2.Begin_timestamp and table2.End_timestamp, it would add "isFound=1". For example, the first row of Table1 is 16:16:50 and is between 16:14:00 and 16:17:02 (the first row of Table2). I want a table like this:

Wanted table

TimestampvalueisFound
2022-07-04 16:16:501201
2022-07-04 16:17:251101
2022-07-04 16:17:351050
2022-07-04 16:17:451301

I tried the following but it only works for the last row.

SELECT t1.timestamp, 
       t1.value,
       (CASE WHEN (t1.Timestamp BETWEEN t2.Begin_Timestamp AND t2.End_Timestamp)
             THEN '1'
        ELSE '0') AS isFound
FROM Table1 t1
LEFT JOIN (
   SELECT * 
   FROM Table2
) AS t2
ON t1.Timestamp BETWEEN t2.Begin_Timestamp AND t2.End_Timestamp 
)

RESULT :

TimestampvalueisFound
2022-07-04 16:16:50120NULL
2022-07-04 16:17:25110NULL
2022-07-04 16:17:35105NULL
2022-07-04 16:17:451301

I use MS-SQL server. Can someone help me with that? I feel like this it is easy but I don't find the problem. Thanks!

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

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

发布评论

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

评论(2

愁杀 2025-02-20 00:27:02

您可以在两个表之间在两个表之间进行左JOIN在“ table1.timestamp ”的条件下,在两个”之间找到了两个” table2 “ timestamps 。

然后,您可以利用具有“ begin_timestamp ”和“ end_timestamp ”等于case> case语句的null的非匹配行“ isfound ”字段。

SELECT table1.*,
       CASE WHEN [Begin_Timestamp] IS NULL THEN 0 ELSE 1 END AS isFound 
FROM      table1
LEFT JOIN table2
       ON table2.[Begin_Timestamp] < table1.[Timestamp] 
      AND table2.[End_Timestamp] > table1.[Timestamp] 

检查演示

Note :此解决方案假设您的所有“ table2 ”时间戳期间彼此之间是不相交的,否则您需要添加dinters关键字选择关键字。

You can do a LEFT JOIN between the two tables on the conditions that the "Table1.Timestamp" is found between the two "Table2" timestamps.

Then you can exploit the non-matching rows having both "Begin_Timestamp" and "End_Timestamp" equal to NULL inside your CASE statement for the creation of the "isFound" field.

SELECT table1.*,
       CASE WHEN [Begin_Timestamp] IS NULL THEN 0 ELSE 1 END AS isFound 
FROM      table1
LEFT JOIN table2
       ON table2.[Begin_Timestamp] < table1.[Timestamp] 
      AND table2.[End_Timestamp] > table1.[Timestamp] 

Check the demo here.

Note: this solution assumes that all your "table2" timestamp periods are disjoint among each other, otherwise you need to add the DISTINCT keyword right after the SELECT keyword.

迷迭香的记忆 2025-02-20 00:27:02

您可以使用 case 表达式与的存在检查:

select *,
    case when exists (
      select * from t2 
      where t1.Timestamp between t2.Begin_Timestamp and t2.End_Timestamp
    ) then 1 else 0 end IsFound
from t1;

You can use case expression combined with an exists check:

select *,
    case when exists (
      select * from t2 
      where t1.Timestamp between t2.Begin_Timestamp and t2.End_Timestamp
    ) then 1 else 0 end IsFound
from t1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文