pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Pól Ua L. <dragam@protonmail.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Joining with calendar table
Date: Tue, 19 Apr 2022 11:37:35 -0700
Message-ID: <150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com> (raw)
In-Reply-To: <foxk5R7fJ6dretSKWPR1Dz2HNNgnX6K_QzjWjDOZxOYH3MGSNLg-1d_pK-Su7zTYP0kh_QAD1UnosyFQXm0Jkwlg3QfmISA8_pm485DRmWo=@protonmail.com>
References: <foxk5R7fJ6dretSKWPR1Dz2HNNgnX6K_QzjWjDOZxOYH3MGSNLg-1d_pK-Su7zTYP0kh_QAD1UnosyFQXm0Jkwlg3QfmISA8_pm485DRmWo=@protonmail.com>

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.klaver@aklaver.com





view thread (2+ messages)

Message-ID: <150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com>
Permalink:  ../150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com/
Also on:    postgresql.org/message-id/150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, dragam@protonmail.com
  Subject: Re: Joining with calendar table
  In-Reply-To: <150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com>
  References: <foxk5R7fJ6dretSKWPR1Dz2HNNgnX6K_QzjWjDOZxOYH3MGSNLg-1d_pK-Su7zTYP0kh_QAD1UnosyFQXm0Jkwlg3QfmISA8_pm485DRmWo=@protonmail.com> <150d21a4-4fcf-dd4d-85ea-063194d89d7c@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox