簡體   English   中英

如何在PostgreSQL中獲取每組最新gather_time(name,col1)的總和(值)?

[英]How can I get the sum(value) on the latest gather_time per group(name,col1) in PostgreSQL?

實際上,我對下面的線程中的類似問題得到了一個很好的答案,但我還需要一個針對不同數據集的解決方案。

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

數據集包含歷史數據,我只想在最新的gather_time上獲取該組的總和(值)。 最終結果應如下:

 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

一種選擇是將原始表連接到一個表,該表僅包含每個name col1組的最新gather_time記錄。 然后,您可以獲取每個組的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM