简体   繁体   English

对时间戳类型的列使用 DISTINCT ON 来提取每天的最新值

[英]Using DISTINCT ON for a column of type timestamp to extract the most recent value for each day

I have a table named assets:我有一个名为资产的表:

create table assets (
    id            bigint                                 primary key,
    name          varchar(255)                           not null,
    value         double precision                       not null,
    business_time timestamp with time zone,
    insert_time   timestamp with time zone default now() not null
);

I am trying to write a query to get the most recent value for each day based on business_time .我正在尝试编写一个查询以根据business_time获取每天的最新value This is a sample query that I wrote:这是我写的示例查询:

select distinct on (business_time::date) business_time::date, value
from home.assets
where name = 'USD_RLS'
order by business_time::date desc

But the value for each day is not always the most recent one.但是每一天的值并不总是最新的。 I guess it's a bit random which value I get for each day.我想我每天得到的值有点随机。 Any idea how I could fix this query?知道如何解决此查询吗?

Here's a good way to approach this requirement, but not with DISTINCT ON.这是满足此要求的好方法,但不是使用 DISTINCT ON。

Start by getting the maximum value of the business_time timestamp value (including date and time) for each calendar day, thusly.因此,首先获取每个日历日的business_time时间戳值(包括日期和时间)的最大值。

             SELECT MAX(business_time) business_time,
                    business_time::date day
               FROM assets
              WHERE name = 'USD_RLS'
              GROUP BY business_time::date

This subquery returns the latest timestamp for each calendar day.此子查询返回每个日历日的最新时间戳。

Then use that list of timestamps to get the detail records you need.然后使用该时间戳列表获取所需的详细记录。

SELECT value, business_time::date day
  FROM assets
 WHERE business_time IN (
                 SELECT MAX(business_time) business_time
                   FROM assets
                  WHERE name = 'USD_RLS'
                  GROUP BY business_time::date )
   AND name = 'USD_RLS'
 ORDER BY business_time::date;

It's all about sets.这都是关于集合的。 First you find the set of timestamps that are the latest for each calendar day.首先,您找到每个日历日最新的一组时间戳。 Then you find the set of rows that have those timestamps.然后您会找到具有这些时间戳的行集。

What you are facing is truncating the time from the timestamp, thus every row for a date has the same time (00:00:00).您面临的是从时间戳中截断时间,因此日期的每一行都具有相同的时间 (00:00:00)。 You can use a slightly unorthodox, but in this case effective sorting.您可以使用稍微非正统但在本例中有效的排序。 Sort by the date (ascending or descending - it does not matter) then (the unorthodox part) sort by the full timestamp desc (must be descending).按日期排序(升序或降序 - 无关紧要)然后(非正统部分)按完整时间戳 desc 排序(必须降序)。 That will give the the latest time for each date.这将给出每个日期的最晚时间。

select distinct on (business_time::date) 
       business_time, value
  from assets
 where name = 'USD_RLS'
 order by business_time::date, business_time desc;

NOTE: Not tested, no data supplied.注意:未经测试,未提供数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM