pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Joining with calendar table
2+ messages / 2 participants
[nested] [flat]

* Joining with calendar table
@ 2022-04-19 07:34  Pól Ua L. <dragam@protonmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Pól Ua L. @ 2022-04-19 07:34 UTC (permalink / raw)
  To: pgsql-general

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?

There is a fiddle here with the code explained above.

https://dbfiddle.uk/?rdbms=postgres_14&fiddle=9a0220303051dd5da57cd1bd5b132e94

Thank you for your time and attentioin - salutations.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Joining with calendar table
@ 2022-04-19 18:37  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Pól Ua L. <dragam@protonmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Adrian Klaver @ 2022-04-19 18:37 UTC (permalink / raw)
  To: Pól Ua L. <dragam@protonmail.com>; 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.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-04-19 18:37 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-04-19 07:34 Joining with calendar table Pól Ua L. <dragam@protonmail.com>
2022-04-19 18:37 ` Adrian Klaver <adrian.klaver@aklaver.com>

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