如何在PostgreSQL中每个组(名称,col1)的最新collect_time上获得总和(值)?

奇异性

实际上,对于以下线程上的类似问题,我得到了很好的答案,但是我需要针对不同数据集的另一种解决方案。

如何获取最新的2行(PostgreSQL)

数据集具有历史数据,我只想在最新的collect_time上获取该组的sum(value)。最终结果应如下:

 name  | col1 |     gather_time     | sum
-------+------+---------------------+-----
 first | 100  | 2016-01-01 23:12:49 |   6
 first | 200  | 2016-01-01 23:11:13 |   4

但是,我只能通过下面的查询看到一个组(前100个)的数据,这意味着第二组(前200个)没有数据。事情是,我需要每个小组一行。组的数量可以变化。

select name,col1,gather_time,sum(value) 
from testtable
group by name,col1,gather_time
order by gather_time desc
limit 2;

 name  | col1 |     gather_time     | sum
-------+------+---------------------+-----
 first | 100  | 2016-01-01 23:12:49 |   6
 first | 100  | 2016-01-01 23:11:19 |   6
(2 rows)

您能建议我完成这项要求吗?

资料集

create table testtable
(
name varchar(30),
col1 varchar(30),
col2 varchar(30),
gather_time timestamp,
value integer
);


insert into testtable values('first','100','q1','2016-01-01 23:11:19',2);
insert into testtable values('first','100','q2','2016-01-01 23:11:19',2);
insert into testtable values('first','100','q3','2016-01-01 23:11:19',2);
insert into testtable values('first','200','t1','2016-01-01 23:11:13',2);
insert into testtable values('first','200','t2','2016-01-01 23:11:13',2);
insert into testtable values('first','100','q1','2016-01-01 23:11:11',2);
insert into testtable values('first','100','q1','2016-01-01 23:12:49',2);
insert into testtable values('first','100','q2','2016-01-01 23:12:49',2);
insert into testtable values('first','100','q3','2016-01-01 23:12:49',2);

select * 
from testtable 
order by name,col1,gather_time;

 name  | col1 | col2 |     gather_time     | value
-------+------+------+---------------------+-------
 first | 100  | q1   | 2016-01-01 23:11:11 |     2
 first | 100  | q2   | 2016-01-01 23:11:19 |     2
 first | 100  | q3   | 2016-01-01 23:11:19 |     2
 first | 100  | q1   | 2016-01-01 23:11:19 |     2
 first | 100  | q3   | 2016-01-01 23:12:49 |     2
 first | 100  | q1   | 2016-01-01 23:12:49 |     2
 first | 100  | q2   | 2016-01-01 23:12:49 |     2
 first | 200  | t2   | 2016-01-01 23:11:13 |     2
 first | 200  | t1   | 2016-01-01 23:11:13 |     2
蒂姆·比格莱森(Tim Biegeleisen)

一种选择是你原来的表连接到只包含有最新的记录的表gather_time的每个namecol1组。然后,您可以取value每个组总和以获得所需的结果集。

SELECT t1.name, t1.col1, MAX(t1.gather_time) AS gather_time, SUM(t1.value) AS sum
FROM testtable t1 INNER JOIN
(
    SELECT name, col1, col2, MAX(gather_time) AS maxTime
    FROM testtable
    GROUP BY name, col1, col2
) t2
ON t1.name = t2.name AND t1.col1 = t2.col1 AND t1.col2 = t2.col2 AND
    t1.gather_time = t2.maxTime
GROUP BY t1.name, t1.col1

如果要在WHERE子句中使用子查询(如在OP中尝试的那样)以将记录限制为仅包含最新记录,gather_time则可以尝试以下操作:

SELECT name, col1, gather_time, SUM(value) AS sum
FROM testtable t1
WHERE gather_time =
(
    SELECT MAX(gather_time) 
    FROM testtable t2
    WHERE t1.name = t2.name AND t1.col1 = t2.col1
)
GROUP BY name, col1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在每个 B 组中选择 A 列的唯一值并获得 C 列中这些唯一值的值的总和

SQL Server:查询以获取表 2 的 Col2 中条件的 Table1 的 Col1 中的值的总和

列出每个 col1 值和多个 col2 值的 col1 和 col2

如何在R中的每个组中选择“ x”个最新值?

如何在MDX的层次结构中查找每个组的总和

如何在mongodb中获取每个组的最新记录?

如果它们的值在 col2 但不在 Python Pandas 的列表中,如何从 col1 中选择值?

CSV文件按col1分组并显示col2的最大值

如何获得桌子上每个循环的总和

如何从每个组中获得前1名

如何从每个组中获得前 1 条记录

如何在java中的地图中获得对(键)值的总和?

如何在Spark中的每个分区上找到总和

根据其他列的值计算 pandas 中的列(如果 col1<col2 和 col2 > col1,则 col3 = 1)

如何在具有多个条件的多列 col0、col1、col2 上使用替换方法

如何在PostgreSQL中获得每个等级的平均行数?

给定匹配col1的值数组,numpy获取col2中的值

从col1中的日期开始的awk组年和月,以及col2中的组之和

如何“插入表1(col1,col2)值,从表2选择col1,从表3选择col2”

我如何从col2获得最早的日期,该日期是col1中具有相同类别的7天之内?

如果小于或等于列表中的值,如何获得每个值的频率和频率的总和?

在由col1分组时获取col2的第二个最大值

如何从表WHERE col1 +“” + col2 LIKE“ John D%”中创建SELECT col1,col2?

如何在R中按组获得计数(滚动总和)?

折叠2列数据框,其中col1包含名称,col2包含值

AWK脚本:查找Col2中每个元素在Col1中具有的匹配数

如何在PostgreSQL中的数组上逐元素应用聚合函数,例如在一个组上加权数组的总和?

如何在DAX中每个列的每个不同值获取列的最新值

如果col 1与col 3相同,如何用X替换col 4中的值,否则使用awk用col 2替换col 4中的每个条目