gpt4 book ai didi

使用交叉表的 postgresql 枢轴

转载 作者:行者123 更新时间:2023-11-29 12:48:52 25 4
gpt4 key购买 nike

我在 postgresql-11 中使用 crosstab() 时遇到问题。

这是我的 table ,

CREATE TABLE monitor(tz timestamptz, level int, table_name text, status text);

该表监视其他表上的事件。它包含

table_name (table on which the event occurred)
timestamp(time at which the event occurred)
level (level of the event)
status of the event (start/end of the event)

这是它的示例数据。

                tz                | level | status |  table_name  
----------------------------------+-------+--------+--------------
2019-10-24 16:18:34.89435+05:30 | 2 | start | test_table_2
2019-10-24 16:18:58.922523+05:30 | 2 | end | test_table_2
2019-11-01 10:31:08.948459+05:30 | 3 | start | test_table_3
2019-11-01 10:41:22.863529+05:30 | 3 | end | test_table_3
2019-11-01 10:51:44.009129+05:30 | 3 | start | test_table_3
2019-11-01 12:35:23.280294+05:30 | 3 | end | test_table_3

给定一个时间戳,我想列出当时所有的当前事件。可以使用标准来完成,

start_time >= 'given_timestamp' and end_time <= 'given_timestamp'

所以我尝试使用 crosstab() 将表旋转到列 table_name、status 和 timestamp。我的问题是,

with q1 (table_name, start_time,end_time) as
(select * from crosstab
('select table_name, status, tz from monitor ')
as finalresult (table_name text, start_time timestamptz, end_time timestamptz)),
q2 (level,start_time,end_time) as
(select * from crosstab('select level, status, tz from monitor ')
as finalresult (level int, start_time timestamptz, end_time timestamptz))
select q1.table_name,q2.level,q1.start_time,q1.end_time
from q1,q2
where q1.start_time=q2.start_time;

查询的输出是,

 table_name  | level |            start_time            |             end_time             
--------------+-------+----------------------------------+----------------------------------
test_table_2 | 2 | 2019-10-24 16:18:34.89435+05:30 | 2019-10-24 16:18:58.922523+05:30
test_table_3 | 3 | 2019-11-01 10:31:08.948459+05:30 | 2019-11-01 10:41:22.863529+05:30

但我的预期输出是,

table_name  | level |            start_time            |             end_time             
--------------+-------+----------------------------------+----------------------------------
test_table_2 | 2 | 2019-10-24 16:18:34.89435+05:30 | 2019-10-24 16:18:58.922523+05:30
test_table_3 | 3 | 2019-11-01 10:31:08.948459+05:30 | 2019-11-01 10:41:22.863529+05:30
test_table_3 | 3 | 2019-11-01 10:51:44.009129+05:30 | 2019-11-01 12:35:23.280294+05:30

如何实现预期的输出?或者有没有比交叉表更好的方法?

最佳答案

我会为此使用自连接。要将行保持在同一级别和表格中,您可以使用窗口函数为它们分配数字,以便区分它们。

with numbered as (
select tz, level, table_name, status,
row_number() over (partition by table_name, status order by tz) as rn
from monitor
)
select st.table_name, st.level, st.tz as start_time, et.tz as end_time
from numbered as st
join numbered as et on st.table_name = et.table_name
and et.status = 'end'
and et.level = st.level
and et.rn = st.rn
where st.status = 'start'
order by st.table_name, st.level;

这假设永远不会有带有 status = 'end' 的行和比带有 status = 'start' 的相应行更早的时间戳>

在线示例:https://rextester.com/QYJK57764

关于使用交叉表的 postgresql 枢轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58655250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com