Skip to content

Instantly share code, notes, and snippets.

@hansthen
Created May 18, 2023 06:49
Show Gist options
  • Save hansthen/b90afc489f102a8923c751d177ddd62b to your computer and use it in GitHub Desktop.
Save hansthen/b90afc489f102a8923c751d177ddd62b to your computer and use it in GitHub Desktop.
create materialized view mview
as
select a.userid, a.balance, d.date
from (select d as date from generate_series(timestamp '2023-05-01', '2023-06-2', interval '1 day') d) d
join lateral (select userid, balance, date from balance where date <= d.date order by date desc limit 1) a
on true union select userid, balance, date from balance order by date;
create materialized view mview2
as
select a.userid, a.balance, a.date as start, b.date as fin
from mview a
left join lateral
(select date
from mview
where date > a.date and
userid = a.userid
order by date asc limit 1)
b on true
create or replace function extract_time(p_timestamp timestamp) returns time
as
$$
declare p_time time := p_timestamp::time;
begin
return case p_time when '00:00:00'::time then '24:00:00'::time else p_time end;
end;
CREATE FUNCTION time_subtype_diff(x time, y time) RETURNS float8 AS
'SELECT EXTRACT(EPOCH FROM (x - y))' LANGUAGE sql STRICT IMMUTABLE;
CREATE TYPE timerange AS RANGE (
subtype = time,
subtype_diff = time_subtype_diff
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment