Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:55:43+00:00 2026-06-13T18:55:43+00:00

I have a table that contains data for every day in 2002, but it

  • 0

I have a table that contains data for every day in 2002, but it has some missing dates. Namely, 354 records for 2002 (instead of 365). For my calculations, I need to have the missing data in the table with Null values

+-----+------------+------------+
| ID  |  rainfall  | date       |
+-----+------------+------------+
| 100 |  110.2     | 2002-05-06 |
| 101 |  56.6      | 2002-05-07 |
| 102 |  65.6      | 2002-05-09 |
| 103 |  75.9      | 2002-05-10 |
+-----+------------+------------+

you see that 2002-05-08 is missing. I want my final table to be like:

+-----+------------+------------+
| ID  |  rainfall  | date       |
+-----+------------+------------+
| 100 |  110.2     | 2002-05-06 |
| 101 |  56.6      | 2002-05-07 |
| 102 |            | 2002-05-08 |
| 103 |  65.6      | 2002-05-09 |
| 104 |  75.9      | 2002-05-10 |
+-----+------------+------------+

Is there a way to do that in PostgreSQL?

It doesn’t matter if I have the result just as a query result (not necessarily an updated table)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:55:45+00:00Added an answer on June 13, 2026 at 6:55 pm

    date is a reserved word in standard SQL and the name of a data type in PostgreSQL. PostgreSQL allows it as identifier, but that doesn’t make it a good idea. I use thedate as column name instead.

    Don’t rely on the absence of gaps in a surrogate ID. That’s almost always a bad idea. Treat such an ID as unique number without meaning, even if it seems to carry certain other attributes most of the time.

    In this particular case, as @Clodoaldo commented, thedate seems to be a perfect primary key and the column id is just cruft – which I removed:

    CREATE TEMP TABLE tbl (thedate date PRIMARY KEY, rainfall numeric);
    INSERT INTO tbl(thedate, rainfall) VALUES
      ('2002-05-06', 110.2)
    , ('2002-05-07', 56.6)
    , ('2002-05-09', 65.6)
    , ('2002-05-10', 75.9);
    

    Query

    Full table by query:

    SELECT x.thedate, t.rainfall  -- rainfall automatically NULL for missing rows
    FROM (
       SELECT generate_series(min(thedate), max(thedate), '1d')::date AS thedate
       FROM   tbl
       ) x
    LEFT   JOIN tbl t USING (thedate)
    ORDER  BY x.thedate
    

    Similar to what @a_horse_with_no_name posted, but simplified and ignoring the pruned id.

    Fills in gaps between first and last date found in the table. If there can be leading / lagging gaps, extend accordingly. You can use date_trunc() like @Clodoaldo demonstrated – but his query suffers from syntax errors and can be simpler.

    INSERT missing rows

    The fastest and most readable way to do it is a NOT EXISTS anti-semi-join.

    INSERT INTO tbl (thedate, rainfall)
    SELECT x.thedate, NULL
    FROM (
       SELECT generate_series(min(thedate), max(thedate), '1d')::date AS thedate
       FROM   tbl
       ) x
    WHERE NOT EXISTS (SELECT 1 FROM tbl t WHERE t.thedate = x.thedate)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that contains some user data: user_id | guest_id | time_seen
I have a table of data for a particular stock that contains every single
I have a table that loads new data every day and another table that
I have a table that contains all the data about users . Users can
I have a table that contains procedure codes among other data (let's call it
I have an application with a table view that contains 3 sections. Every thing
I have a table that contains partial data that is of no use to
I have a table which contains data recorded every minute, so I have a
I have a table with well over 5 millions rows, that contains hierarchical data
I have a table that contains multiple dropdown menus for a list of profile

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.