SQL在时间戳之间加入
我有两个这样的桌子:
table1
时间戳 | 值 |
---|---|
2022-07-04 16:16:50 | 120 120 |
2022-07-07-04 16:17:25 | 110 |
2022-07-07-07-04 16:17:35 105 | 105 105 |
2022-07-04 16:17:45 | 130 |
table2
begin_timestamp | end_timestamp |
---|---|
2022-07-04 16:14:00 | 2022-07-04 16:17:02 |
2022-07-04 16:17:17:11 | 2022-07-07-04 16:17:17:30 |
2022-07-04 16:17:38 | 2022-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:50 | 120 120 | 1 |
2022-07-04 16:17:25 | 110 | 1 |
2022-07-07-04 16: 17:35 | 105 | 0 |
2022-07-04 16:17:45 | 130 | 1 |
我尝试了以下内容,但仅适用于最后一行。
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:50 | 120 | NAULL |
2022-07-04 16:17:25 | 110 | NULL |
2022-07-07-04 16:17:17:17:17:17: | 17: 35 105 | NAULL |
20222-07-07-04 16:17: 45 | 130 | 1 |
我使用MS-SQL Server。有人可以帮我吗?我觉得这很容易,但我没有发现问题。谢谢!
I have two tables like these:
Table1
Timestamp | value |
---|---|
2022-07-04 16:16:50 | 120 |
2022-07-04 16:17:25 | 110 |
2022-07-04 16:17:35 | 105 |
2022-07-04 16:17:45 | 130 |
Table2
Begin_Timestamp | End_Timestamp |
---|---|
2022-07-04 16:14:00 | 2022-07-04 16:17:02 |
2022-07-04 16:17:11 | 2022-07-04 16:17:30 |
2022-07-04 16:17:38 | 2022-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
Timestamp | value | isFound |
---|---|---|
2022-07-04 16:16:50 | 120 | 1 |
2022-07-04 16:17:25 | 110 | 1 |
2022-07-04 16:17:35 | 105 | 0 |
2022-07-04 16:17:45 | 130 | 1 |
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 :
Timestamp | value | isFound |
---|---|---|
2022-07-04 16:16:50 | 120 | NULL |
2022-07-04 16:17:25 | 110 | NULL |
2022-07-04 16:17:35 | 105 | NULL |
2022-07-04 16:17:45 | 130 | 1 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在两个表之间在两个表之间进行
左JOIN
在“ table1.timestamp ”的条件下,在两个”之间找到了两个” table2 “ timestamps 。然后,您可以利用具有“ begin_timestamp ”和“ end_timestamp ”等于
case> case
语句的null的非匹配行“ isfound ”字段。检查演示
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.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 theSELECT
keyword.您可以使用 case 表达式与的存在检查:
You can use case expression combined with an exists check: