Home > Back-end >  How to insert a several months of dates, day-by-day in a date type column?
How to insert a several months of dates, day-by-day in a date type column?

Time:06-18

I need to create a new table for reporting which lists the number of accounts on any day. The original Accounts table contains the created_date column so if I want to know how many accounts I have today, I need to execute:

select count(*) from Accounts where created_date > CURRENT_DATE;

But how can I create the list of all dates e.g. starting from 2022-01-01 to compare the created date with each of them?

CodePudding user response:

To create list of dates you can use such expression:

SELECT 
    t.date::date
FROM generate_series(timestamp '2022-01-01',
                     current_date,
                     interval  '1 day') AS t(date);

If you need a help with comparing, please provide info with expecting result.

  • Related