2017-07-13 38 views
0

我有相同的時間戳2個不同類型的總值。我想創建一個查詢,其中檢索timestamp,value1,value2其中value1是類型1的總和,value2是類型2的總和。得到不同的值爲相同的時間戳postgres

我的總和字段是一個數值[],但在我正在處理的類型中只有一個值..在其他情況下是具有不同值的數組。

我想:

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 

我要像時間戳,類型,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 

有人嗎?

+0

提示:'GROUP BY時間戳,類型' –

回答

0

,您是否試圖總和[1] ..是這樣的:

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

,並讓這一切在一列,只是conat結果:

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 
+0

列「default_dataset.timestamp」必須出現在GROUP BY子句中或用於聚合函數 – ricardol

+0

我的壞 - 編輯 –

+0

我運行您的查詢,我收到錯誤... – ricardol

相關問題