timestamp | data
-----------------------------------------------------------------
2017-12-10 10:33:19.563241+00 | {"foo":900}
2017-12-11 10:34:19.563241+00 | {"foo":1800}
2017-12-11 10:35:19.563241+00 | {"foo":1800,"bar":400}
2017-12-11 10:36:19.563241+00 | {"foo":2400,"bar":400}
day | data
--------------------------------------------------------
2017-12-11 00:00:00+00 | {"foo":2400,"bar":400}
2017-12-10 00:00:00+00 | {"foo":900}
SELECT DISTINCT "timestamp", date_trunc('day', "timestamp") AS "day", FIRST_VALUE (data) OVER (PARTITION BY "timestamp")
FROM "table"
ORDER BY "timestamp" DESC
select day, data
from generate_series('2017-12-10', '2017-12-12', interval '1 day') as day,
lateral (
select data from tablename
where "timestamp" between day and day + interval '1 day'
order by "timestamp" desc limit 1
) ljd;
with recursive t as (
(select "timestamp"::date as day, data from tablename order by "timestamp" desc limit 1)
union all
select bpt.* from t, lateral (
select "timestamp"::date as day, data from tablename where "timestamp" < t.day order by "timestamp" desc limit 1
) as bpt
)
select * from t;
SELECT
date_trunc('day', sub."timestamp") AS "day",
sub. data
FROM (SELECT
"timestamp",
"data",
row_number() OVER ( PARTITION BY date_trunc('day', "timestamp")
ORDER BY "timestamp" DESC ) AS rownum
FROM "table") sub
WHERE sub.rownum = 1
SELECT
DISTINCT date_trunc('day', "s"."timestamp") AS "day",
FIRST_VALUE("s"."data"::jsonb) OVER (PARTITION BY date_trunc('day', "s"."timestamp")) AS "dat"
FROM
(SELECT "data", "timestamp" FROM "table" ORDER BY "timestamp" DESC) AS "s"
ORDER BY "day" DESC
commented on June 10th 19 at 16:13select
*
from table
inner join (
select
dt=max(timestamp)
from table
group by date_trunc('day', "timestamp") -- hand mssql - there yuzal cast(... as date)
) as t on t.dt=table.timestamp
Find more questions by tags SQLPostgreSQL
Thanks for the tip, later rewrite using recursively ) - Gonzalo.Hane39 commented on June 10th 19 at 16:08
compared to the "brute force"
Maybe in the future it will be better - Gonzalo.Hane39 commented on June 10th 19 at 16:11
And demand is only a small part of the whole production, there is still some JOIN and UNION this to the whole case. In the end, all the termination is to be run for testing with a lot more data to build necessary indexes.
The main thing that Your advice about recursive queries could affect another request that I could improve, so Thank You still owed - Gonzalo.Hane39 commented on June 10th 19 at 16:20
Usually in such a jungle climb in an attempt to speed up what is already there and where it is possible to rewrite the query drastically. Without an index, under which the request was based - the result will probably be very poor. Recursive queries in General good can shoot you in the leg.
By the way, here is a presentation on several reconsiderations requests from my colleagues and supervisor: https://pgday.ru/presentation/232/5964945ea4142.pdf (record of the report of the conference freely available, unfortunately, not published) - Ignacio_Romaguera commented on June 10th 19 at 16:23