SQL:查询连续三个时间戳并放入列
想象一下,我们有下表,其中0是开始时间,而1是相同x过程的结束时间。
时间 | 0 | 友好 |
---|---|---|
0 | 12:12.34 | x |
1 | 12:12.55 | x |
0 | 12 | x |
1 | 12:15.59 | x |
x | 12:16.07 | x |
1 | 12:16.42 | x |
我想获得结果:
善良的 | 启动 | next | next |
---|---|---|---|
: 15.22 | 12:12.34 | 12: 12.55 | 12:15.22 |
x | 12:15.22 | 12:15.59 | 12:16.07 |
x | 12:16.07 | 12:16.42 | (or等) |
在时间顺序上,前三个事件应在开始,结束,下一个启动列中,因为遵循0 =开始,1 =结束,依此类推。
我这样做了,但是关于next_start有一个错误
SELECT kind, start, end, next_start
FROM(
SELECT kind, F.time as start, S.time as end, max(F.time) as next_start
from Table F
INNER JOIN Table S
ON F.kind = S.kind
WHERE end>start
AND next_start> end
GROUP BY kind, start, end
)
order by next_start
Imagine we have the following table where 0 is a start time, and 1 is an end time of the same kind x process.
way | time | kind |
---|---|---|
0 | 12:12.34 | x |
1 | 12:12.55 | x |
0 | 12:15.22 | x |
1 | 12:15.59 | x |
0 | 12:16.07 | x |
1 | 12:16.42 | x |
I would like get as result:
kind | start | end | next_start |
---|---|---|---|
x | 12:12.34 | 12:12.55 | 12:15.22 |
x | 12:15.22 | 12:15.59 | 12:16.07 |
x | 12:16.07 | 12:16.42 | (so on) |
In time order the first three events should be pone in start, end, next start column because following the way 0 = start, 1= end, and so on.
i did in this way but there is an error regarding next_start
SELECT kind, start, end, next_start
FROM(
SELECT kind, F.time as start, S.time as end, max(F.time) as next_start
from Table F
INNER JOIN Table S
ON F.kind = S.kind
WHERE end>start
AND next_start> end
GROUP BY kind, start, end
)
order by next_start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以尝试使用
sum
窗口函数以获取每个开始和结束时间分组编号,然后使用min
和max
contregate功能以获取启动和结束时间。最终,我们可以尝试使用
Lead
窗口函数获取next_start
列。查询1 :
结果 强>:
We can try to use
SUM
window function to get each start and end time grouping number, then useMIN
andMAX
aggregate function to get the start and end time.Final, we can try to use
LEAD
window function to getnext_start
column.Query 1:
Results: