如何获取最新的2行(PostgreSQL) [英] How to get the latest 2 rows ( PostgreSQL )

查看:133
本文介绍了如何获取最新的2行(PostgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助来修改查询以完成要求。

I need your help to modify a query to accomplish the requirement.

根据GATHER_TIME,我想显示名称的值。
查询每1分钟运行一次,它应该一直获得2行。
如果当时没有数据,则应获取Name的过去(前1分钟)数据。

According to the GATHER_TIME, I want to show the VALUE of the NAMEs. The query runs every 1 minute, and it should get 2 rows all the time. In case, there is no data at that time, it should get the past( 1 minute earlier ) data for the Name.

这里,具体要求是我不应该在时间戳记值(GATHER_TIME)中考虑秒值。

Here, specific requirement is that I should not consider seconds value in Timestamp value(GATHER_TIME).

我试图进行如下查询。
它只会获取最新数据。

I tried to make a query like below. It only gets the latest data.

您能帮帮我吗?

select NAME,sum(VALUE)
FROM TestTable
WHERE substring(to_char(gather_time,'YYYY-MM-DD HH24:MI:SS'),1,16) IN ( SELECT MAX(substring(to_char(gather_time,'YYYY-MM-DD HH24:MI:SS'),1,16)) FROM TestTable )
GROUP BY NAME



表格数据



Table Data

NAME    COL1   COL2    GATHER_TIME         VALUE
------------------------------------------------
first   prince PQ1     2015-12-29 13:10:33 11
first   prince PQ2     2015-12-29 13:10:33 14
first   prince PQ3     2015-12-29 13:10:33 18
first   prince PQ4     2015-12-29 13:10:33 19
second  prince TT1     2015-12-29 13:10:59 20
second  prince TT2     2015-12-29 13:10:59 29
second  prince TT3     2015-12-29 13:10:59 43
first   prince PQ1     2015-12-29 13:11:37 71
first   prince PQ2     2015-12-29 13:11:37 74
first   prince PQ3     2015-12-29 13:11:37 78
first   prince PQ4     2015-12-29 13:11:37 79



必需结果



Required result

@ Query time: 2015-12-29 13:10:59
first       62
second      92

@ Query time: 2015-12-29 13:11:59
first       302
second      92



已添加:



对不起。我应该放更详细的测试数据。

Added:

Sorry. I should have put more detail test data. Updated it.

SELECT DISTINCT ON (name,col1,gather_time) name,col1,SUM(value) 
FROM test_table
GROUP BY name,col1,gather_time
ORDER BY gather_time DESC
LIMIT 2;


推荐答案

-- DDL
CREATE TABLE test_table(
  name TEXT,
  col2 TEXT,
  gather_time TIMESTAMP,
  value INTEGER
);

--sample data
INSERT INTO test_table  VALUES
  ('first','PQ1','2015-12-29 13:10:33'::TIMESTAMP, 11),
  ('first','PQ2','2015-12-29 13:10:33'::TIMESTAMP, 14),
  ('first','PQ3','2015-12-29 13:10:33'::TIMESTAMP, 18),
  ('first','PQ4','2015-12-29 13:10:33'::TIMESTAMP, 19),
  ('second','TT1','2015-12-29 13:10:59'::TIMESTAMP, 20),
  ('second','TT2','2015-12-29 13:10:59'::TIMESTAMP, 29),
  ('second','TT3','2015-12-29 13:10:59'::TIMESTAMP, 43),
  ('first','PQ1','2015-12-29 13:11:37'::TIMESTAMP, 71),
  ('first','PQ2','2015-12-29 13:11:37'::TIMESTAMP, 74),
  ('first','PQ3','2015-12-29 13:11:37'::TIMESTAMP, 78),
  ('first','PQ4','2015-12-29 13:11:37'::TIMESTAMP, 79);

--query to run every minute
SELECT name,SUM(value) FROM test_table
  WHERE gather_time <= now()
  GROUP BY name,gather_time
  ORDER BY gather_time DESC
  LIMIT 2;

或使用以下功能:

CREATE OR REPLACE FUNCTION get_2latest_agg_rows(qtime timestamp) 
  RETURNS TABLE (
    order_name TEXT,
    sum_value BIGINT
 ) AS $$
BEGIN
  RETURN QUERY
  SELECT name as order_name,SUM(value) as agg_result FROM test_table
  WHERE gather_time <= qtime::TIMESTAMP
  GROUP BY name,gather_time
  ORDER BY gather_time DESC
  LIMIT 2;
END;
$$ LANGUAGE plpgsql;

--sample run
SELECT * FROM get_2latest_agg_rows('2015-12-29 13:11:59'::TIMESTAMP);

这篇关于如何获取最新的2行(PostgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆