我有一张具有以下结构的桌子。
+ ----------------------+--------------+--------+ | timeStamp | value | type | + ----------------------+--------------+--------+ | '2010-01-14 00:00:00' | '11787.3743' | 'mean' | | '2018-04-03 14:19:21' | '9.9908' | 'std' | | '2018-04-03 14:19:21' | '11787.3743' | 'min' | + ----------------------+--------------+--------+
现在我想写一个select查询,我可以根据类型获取数据。
+ ----------------------+--------------+-------------+----------+ | timeStamp | mean_type | min_type | std_type | + ----------------------+--------------+-------------+----------+ | '2010-01-14 00:00:00' | '11787.3743' | | | | '2018-04-03 14:19:21' | | | '9.9908' | | '2018-04-03 14:19:21' | | '11787.3743 | | + ----------------------+--------------+-------------+----------+
请帮助我如何通过编写查询在postgres数据库中执行此操作。我还想以10分钟的间隔获取数据。
答案 0 :(得分:4)
使用with my_table(timestamp, value, type) as (
values
('2010-01-14 00:00:00', 11787.3743, 'mean'),
('2018-04-03 14:19:21', 9.9908, 'std'),
('2018-04-03 14:19:21', 11787.3743, 'min')
)
select
timestamp,
case type when 'mean' then value end as mean_type,
case type when 'min' then value end as min_type,
case type when 'std' then value end as std_type
from my_table;
timestamp | mean_type | min_type | std_type
---------------------+------------+------------+----------
2010-01-14 00:00:00 | 11787.3743 | |
2018-04-03 14:19:21 | | | 9.9908
2018-04-03 14:19:21 | | 11787.3743 |
(3 rows)
:
declare
v_ids int[];
begin
select array_agg(id) INTO v_ids
from mytable1
where host = p_host;
--use v_ids...
end;