Re: Joining with calendar table

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Pól Ua L(dot) <dragam(at)protonmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Joining with calendar table
Date: 2022-04-19 18:37:35
Message-ID: 150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 4/19/22 00:34, Pól Ua L. wrote:
>
>
> Bonjour a tous/Hello all,
>
>
> Small problem - I hope it not to trivial for here.
>
> We created a table, then someone deleted some records and we want to put
> them back, but we not sure how.
>
> CREATE TABLE dat AS
>   SELECT
>     GENERATE_SERIES
>     (
>       '2022-03-01'::DATE,
>       '2022-04-18'::DATE,
>       '1 DAY'
>     ) AS jour;
>
> So, all days from March 01 to Easter Monday.
>
> Then someone delete the weekends.
>
>
> delete from dat where extract(isodow from jour) IN (6, 7);
>
>
>
> But, there are also actvities on these days  also, so I would like to
> put them back in.
>
> I think I need a left join with the calendar table, but am not sure how
> to do this so we have full months again.

>
> Could someone show me how this to be done please?

BEGIN;
insert into dat select j.a from GENERATE_SERIES
(
'2022-03-01'::DATE,
'2022-04-18'::DATE,
'1 DAY'
) as j(a) left join dat on j.a = dat.jour where dat.jour is null;

INSERT 0 14

Verify the dates where added then:

COMMIT;

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2022-04-19 19:29:04 psql timeout: who's waiting for whom
Previous Message Adrian Klaver 2022-04-19 17:20:51 Re: Huge archive log generate in Postgresql-13