获得相同时间戳postgres

时间:2017-07-13 11:24:43

标签: sql postgresql

我有相同的时间戳2种不同类型的总价值。我想创建一个查询,它检索timestamp,value1,value2,其中value1是type1的总和,value2是type2的总和。

我的sum字段是一个数字[],但我正在处理的类型只有一个值..在其他情况下是一个具有不同值的数组。

我试过了:

select distinct type, timestamp, sum 
from default_dataset 
where type like 'probing_%' 
group by type, timestamp, sum 
limit 10

否则

select timestamp, type, sum 
from default_dataset 
where type like 'probing_%' 
order by timestamp desc 
limit 50

我有

2017-07-13 12:24:38+01 | probing_repated | 50
2017-07-13 12:24:38+01 | probing_live | 10
2017-07-13 12:24:38+01 | probing_live | 15
2017-07-13 12:24:38+01 | probing_repated | 10
2017-07-13 12:19:00+01 | probing_live | 11
2017-07-13 12:19:00+01 | probing_repeated | 40
2017-07-13 12:19:00+01 | probing_repeated | 21
2017-07-13 12:19:00+01 | probing_live | 26

我想要一个像timestamp,type,sum(sum)

这样的结果
2017-07-13 12:24:38+01 | probing_repated | 65
2017-07-13 12:24:38+01 | probing_live | 25

2017-07-13 12:19:00+01 | probing_live | 37
2017-07-13 12:19:00+01 | probing_repeated | 61

任何?

1 个答案:

答案 0 :(得分:0)

你试过sum [1]吗?..像这样:

select timestamp, type, sum(sum[1])
from default_dataset 
where type like 'probing_%' 
group by "timestamp", "type" 
limit 50

并将其全部放在一列中,只需输入结果:

with a as (
select timestamp a, type b, sum(sum[1]) c
from default_dataset 
where type like 'probing_%' 
group by "timestamp", "type" 
)
select concat(a,' ',b,' ',c) from a