【问题标题】:get diferent values for same timestamp postgres为相同的时间戳 postgres 获取不同的值
【发布时间】:2017-07-13 11:24:42
【问题描述】:

对于相同的时间戳,我有 2 种不同类型的总值。我想创建一个检索时间戳、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

我想要时间戳、类型、总和(总和)等结果

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

有人吗?

【问题讨论】:

  • 提示:GROUP BY timestamp, type

标签: sql postgresql


【解决方案1】:

你尝试过 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

【讨论】:

  • column "default_dataset.timestamp" 必须出现在 GROUP BY 子句中或用于聚合函数中
  • 立即尝试。对不起,我自己 - 你不给关系的 DDL
  • 如果我尝试使用内部连接怎么办?类似于...选择 dd.timestamp,ddl.sum(sum[1]) 作为 live,ddr.sum(sum[1]) 作为重复,ddp.sum(sum[1]) 作为 passerBy from default_dataset dd inner join default_dataset ddl on ddl.timestamp=dd.timestamp 和 ddl.type='probing_live' 内部连接 ​​default_dataset ddr on ddr.timestamp=dd.timestamp 和 ddr.type='probing_repeated' 内部连接 ​​default_dataset ddp on ddp.timestamp=dd.timestamp和 ddp.type='probing_passerBy' 其中 dd.timestamp
  • 好吧,我在这里没有看到加入的任何好处。但是,如果您看到了,请发布一个新问题?
  • 我试图把所有东西放在一起,以简化我的数据。我需要一个带有时间戳totalType1的结构,totalType2明白..我该怎么做?你给我的查询得到了数据但分开了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2020-08-24
相关资源
最近更新 更多