时间间隔后期

发布于 2025-01-21 09:25:30 字数 1976 浏览 6 评论 0原文

我有一个包含ID(唯一),系统ID,时间戳和状态的表。 当状态1的意思是,系统不可用时,当0-可用时:

CREATE temp TABLE temp_data_test (
       id int8 NULL,
       system_id int8 NULL,
       time timestamptz NULL,
       status int4 NULL
);
 
INSERT INTO temp_data_test (id, system, time, status) VALUES
       (53,1,'2022-04-02 13:57:07.000',1),
       (54,1,'2022-04-02 14:10:26.000',0),
       (55,1,'2022-04-02 14:28:45.000',1),
       (56,1,'2022-04-02 14:32:19.000',0),
       (57,1,'2022-04-05 03:20:18.000',1),
       (58,3,'2022-04-05 03:21:18.000',1),
       (59,2,'2022-04-05 03:21:22.000',1),
       (60,2,'2022-04-06 02:27:15.000',0),
       (61,3,'2022-04-06 02:27:15.000',0),
       (62,1,'2022-04-06 02:28:17.000',0);

它的起作用,就像系统变得不可用时,我们会获得1,当可用时获得1-> 0。 当每个系统都无法看到每个系统时,我需要获得一个结果表。 对于此表结果,我应该

date          system                 available                             unavailable
2022-04-02      1      13:57:07+00:18:19+09:27:40 =23:43:06         23:59:59-23:43:06=..
2022-04-02      2                        24                                     0
2022-04-02      3                        24                                     0

2022-04-03      1                        24                                     0
2022-04-03      2                        24                                     0
2022-04-03      3                        24                                     0
...
2022-04-05      1                     03:20:18                     23:59:59-03:20:18=..
2022-04-05      3                     03:21:18                     23:59:59-03:21:18=..
2022-04-05      2                     03:21:22                     23:59:59-03:21:22=..

2022-04-06      1               23:59:59-02:28:17=..                       02:28:17
2022-04-06      3               23:59:59-02:27:15=..                       02:27:15
2022-04-06      2               23:59:59-02:27:15=..                       02:27:15

尝试使用过度划分和递归来进行此操作,但要获得的间隔比我需要更多。

I have a table which contains id (unique), system id, timestamp and status.
When status 1 it means, system unavailable, when 0 - available:

CREATE temp TABLE temp_data_test (
       id int8 NULL,
       system_id int8 NULL,
       time timestamptz NULL,
       status int4 NULL
);
 
INSERT INTO temp_data_test (id, system, time, status) VALUES
       (53,1,'2022-04-02 13:57:07.000',1),
       (54,1,'2022-04-02 14:10:26.000',0),
       (55,1,'2022-04-02 14:28:45.000',1),
       (56,1,'2022-04-02 14:32:19.000',0),
       (57,1,'2022-04-05 03:20:18.000',1),
       (58,3,'2022-04-05 03:21:18.000',1),
       (59,2,'2022-04-05 03:21:22.000',1),
       (60,2,'2022-04-06 02:27:15.000',0),
       (61,3,'2022-04-06 02:27:15.000',0),
       (62,1,'2022-04-06 02:28:17.000',0);

It works like when system become unavailable we get 1, when become available -> 0.
I need to get a result table when can see how much hours each day each system was unavailable.
For this table result should be

date          system                 available                             unavailable
2022-04-02      1      13:57:07+00:18:19+09:27:40 =23:43:06         23:59:59-23:43:06=..
2022-04-02      2                        24                                     0
2022-04-02      3                        24                                     0

2022-04-03      1                        24                                     0
2022-04-03      2                        24                                     0
2022-04-03      3                        24                                     0
...
2022-04-05      1                     03:20:18                     23:59:59-03:20:18=..
2022-04-05      3                     03:21:18                     23:59:59-03:21:18=..
2022-04-05      2                     03:21:22                     23:59:59-03:21:22=..

2022-04-06      1               23:59:59-02:28:17=..                       02:28:17
2022-04-06      3               23:59:59-02:27:15=..                       02:27:15
2022-04-06      2               23:59:59-02:27:15=..                       02:27:15

I try do it with over partition by and recursion, but get more interval, than I need.

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

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

发布评论

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

评论(1

贱贱哒 2025-01-28 09:25:30

一个示例查询,用于计算间隔并使用表结构显示时间:

with d_test as (
    select 
        row_number() over (order by system_id, time) as r_num, 
        "time"::date as "onlydate",
        "time", 
        system_id, 
        status
    from temp_data_test
    order by system_id, time 
)
select avi.*, unavi."unavialible", unavi."unavialible_hours" from (
    select 
        d1.system_id, 
        d1.onlydate,
        sum(d2.time - d1.time) as "avialible",  
        (extract(day from sum(d2.time - d1.time) )*24 + extract(hour from sum(d2.time - d1.time)))::text || ' hours' as "avialible_hours"
    from d_test d1 
    inner join d_test d2 on d1.r_num+1 = d2.r_num and d1.system_id = d2.system_id 
    where d1.status = 1 
    group by d1.system_id, d1.onlydate
    order by d1.system_id 
) avi 
left join (
    select 
        d1.system_id, 
        d1.onlydate,
        sum(d2.time - d1.time) as "unavialible",  
        (extract(day from sum(d2.time - d1.time) )*24 + extract(hour from sum(d2.time - d1.time)))::text || ' hours' as "unavialible_hours"
    from d_test d1 
    inner join d_test d2 on d1.r_num+1 = d2.r_num and d1.system_id = d2.system_id 
    where d1.status = 0 
    group by d1.system_id, d1.onlydate
    order by d1.system_id
) unavi on avi.system_id = unavi.system_id and avi.onlydate = unavi.onlydate 

此查询的结果:

System_idbyse_idavialibleavialible_hoursnovialibleunavialible_hours
12022-04-04-02002 0020时00:16:53我编写了 小时
12022-04-0523:07:5923小时
22022-04-0523:05:5323小时
32022-04-051天02:05:5726小时

I wrote a sample query for calculating intervals and showing intervals as hours using your table structure:

with d_test as (
    select 
        row_number() over (order by system_id, time) as r_num, 
        "time"::date as "onlydate",
        "time", 
        system_id, 
        status
    from temp_data_test
    order by system_id, time 
)
select avi.*, unavi."unavialible", unavi."unavialible_hours" from (
    select 
        d1.system_id, 
        d1.onlydate,
        sum(d2.time - d1.time) as "avialible",  
        (extract(day from sum(d2.time - d1.time) )*24 + extract(hour from sum(d2.time - d1.time)))::text || ' hours' as "avialible_hours"
    from d_test d1 
    inner join d_test d2 on d1.r_num+1 = d2.r_num and d1.system_id = d2.system_id 
    where d1.status = 1 
    group by d1.system_id, d1.onlydate
    order by d1.system_id 
) avi 
left join (
    select 
        d1.system_id, 
        d1.onlydate,
        sum(d2.time - d1.time) as "unavialible",  
        (extract(day from sum(d2.time - d1.time) )*24 + extract(hour from sum(d2.time - d1.time)))::text || ' hours' as "unavialible_hours"
    from d_test d1 
    inner join d_test d2 on d1.r_num+1 = d2.r_num and d1.system_id = d2.system_id 
    where d1.status = 0 
    group by d1.system_id, d1.onlydate
    order by d1.system_id
) unavi on avi.system_id = unavi.system_id and avi.onlydate = unavi.onlydate 

Result of this query:

system_idonlydateavialibleavialible_hoursunavialibleunavialible_hours
12022-04-0200:16:530 hours2 days 13:06:1861 hours
12022-04-0523:07:5923 hours
22022-04-0523:05:5323 hours
32022-04-051 day 02:05:5726 hours
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文