SQL:查询连续三个时间戳并放入列

发布于 2025-02-11 20:26:49 字数 1274 浏览 0 评论 0原文

想象一下,我们有下表,其中0是开始时间,而1是相同x过程的结束时间。

时间0友好
012:12.34x
112:12.55x
012x
112:15.59x
x12:16.07x
112:16.42x

我想获得结果:

善良的启动nextnext
: 15.2212:12.3412: 12.5512:15.22
x12:15.2212:15.5912:16.07
x12:16.0712: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.

waytimekind
012:12.34x
112:12.55x
012:15.22x
112:15.59x
012:16.07x
112:16.42x

I would like get as result:

kindstartendnext_start
x12:12.3412:12.5512:15.22
x12:15.2212:15.5912:16.07
x12:16.0712: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 技术交流群。

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

发布评论

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

评论(1

木有鱼丸 2025-02-18 20:26:49

我们可以尝试使用sum窗口函数以获取每个开始和结束时间分组编号,然后使用minmax contregate功能以获取启动和结束时间。

最终,我们可以尝试使用Lead窗口函数获取next_start列。

查询1

SELECT kind,
       "start",
       "end",
       LEAD("start") OVER(ORDER BY "start") next_start
    FROM (
     SELECT kind,
           MIN("time") "start",
           MAX("time") "end"
    FROM (
        SELECT t1.*,
               SUM(CASE WHEN way = 0 THEN 1 ELSE 0 END) OVER(ORDER BY "time") grp
        FROM "Table" t1 
    ) t1
    GROUP BY kind,grp
) t1

结果 强>:

| KIND |                 start |                   end |            NEXT_START |
|------|-----------------------|-----------------------|-----------------------|
|    x | 2022-06-15 12:12:34.0 | 2022-06-15 12:12:55.0 | 2022-06-15 12:15:22.0 |
|    x | 2022-06-15 12:15:22.0 | 2022-06-15 12:15:59.0 | 2022-06-15 12:16:07.0 |
|    x | 2022-06-15 12:16:07.0 | 2022-06-15 12:16:42.0 |                (null) |

We can try to use SUM window function to get each start and end time grouping number, then use MIN and MAX aggregate function to get the start and end time.

Final, we can try to use LEAD window function to get next_start column.

Query 1:

SELECT kind,
       "start",
       "end",
       LEAD("start") OVER(ORDER BY "start") next_start
    FROM (
     SELECT kind,
           MIN("time") "start",
           MAX("time") "end"
    FROM (
        SELECT t1.*,
               SUM(CASE WHEN way = 0 THEN 1 ELSE 0 END) OVER(ORDER BY "time") grp
        FROM "Table" t1 
    ) t1
    GROUP BY kind,grp
) t1

Results:

| KIND |                 start |                   end |            NEXT_START |
|------|-----------------------|-----------------------|-----------------------|
|    x | 2022-06-15 12:12:34.0 | 2022-06-15 12:12:55.0 | 2022-06-15 12:15:22.0 |
|    x | 2022-06-15 12:15:22.0 | 2022-06-15 12:15:59.0 | 2022-06-15 12:16:07.0 |
|    x | 2022-06-15 12:16:07.0 | 2022-06-15 12:16:42.0 |                (null) |
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文