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 6947723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:47+00:00 2026-05-27T13:43:47+00:00

In a postgresql database I have 2 tables like these ones: Table items: item_id

  • 0

In a postgresql database I have 2 tables like these ones:

Table items:

item_id    item     save_date (date dd/mm/yy)
----------------------------------------------
1          car       01/12/11
2          wheel     10/12/11
3          screen    11/12/11
4          table     15/12/11

Table periods:

period_id  period_name    start (date dd/mm/yy)
------------------------------------------------
1          period1      05/12/11
2          period2      09/12/11
3          period3      12/12/11

So I would like to get a joined table adding to the table items the most suitable period (if on matches). A period will match if the save_date of the item is after the start date of the period.

For example for “car” the date is 01/12/2011 so I go to the table periods and there I can see that all periods start after that date, so I have to set it to null. If I take “screen” the date is 11/12/11 so in the periods I should take period2, because it’s the latest which matches.

So the joined table would result in:

item_id    item     save_date   period_name
----------------------------------------------
1          car       01/12/11      null
2          wheel     10/12/11      period2
3          screen    11/12/11      period2
4          table     15/12/11      period3

What is the best way to do this join?

Thanks

  • 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-05-27T13:43:48+00:00Added an answer on May 27, 2026 at 1:43 pm
    SET search_path='tmp';
    
    DROP TABLE items CASCADE;
    CREATE TABLE items
        ( item_id INTEGER NOT NULL PRIMARY KEY
        , item VARCHAR
        , save_date date NOT NULL
        );
    INSERT INTO items(item_id,item,save_date) VALUES
     ( 1, 'car', '2011-12-01' )
    ,( 2, 'wheel', '2011-12-10' )
    ,( 3, 'screen', '2011-12-11' )
    ,( 4, 'table', '2011-12-15' )
        ;
    
    DROP TABLE periods CASCADE;
    CREATE TABLE periods
        ( period_id INTEGER NOT NULL PRIMARY KEY
        , period_name VARCHAR
        , start_date date NOT NULL
        );
    INSERT INTO periods(period_id,period_name,start_date) VALUES
     ( 1, 'period1', '2011-12-05' )
    ,( 2, 'period2', '2011-12-09' )
    ,( 3, 'period3', '2011-12-12' )
        ;
    -- self-join to find the next interval
    WITH pe AS (
        SELECT p0.period_id,p0.period_name,p0.start_date
            , p1.start_date AS end_date
        FROM periods p0
        -- must be a left join; because the most recent interval is still open
        -- (has no successor)
        LEFT JOIN periods p1 ON p1.start_date > p0.start_date
        WHERE NOT EXISTS (
            SELECT * FROM periods px
            WHERE px.start_date > p0.start_date
            AND px.start_date < p1.start_date
            )
        )
    SELECT it.item_id
        , it.item
        , it.save_date
        , pe.period_id
        , pe.period_name
        , pe.start_date
        , pe.end_date
    FROM items it
    LEFT JOIN pe
           ON it.save_date >= pe.start_date
          AND ( it.save_date < pe.end_date OR pe.end_date IS NULL)
        ;
    

    The result:

     item_id |  item  | save_date  | period_id | period_name | start_date |  end_date
    ---------+--------+------------+-----------+-------------+------------+------------
           1 | car    | 2011-12-01 |           |             |            |
           2 | wheel  | 2011-12-10 |         2 | period2     | 2011-12-09 | 2011-12-12
           3 | screen | 2011-12-11 |         2 | period2     | 2011-12-09 | 2011-12-12
           4 | table  | 2011-12-15 |         3 | period3     | 2011-12-12 |
    (4 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really big database (running on PostgreSQL) containing a lot of tables
In PostgreSQL 8.3 database I have bookings table referencing booking_transactions table by ID. So
I have a name of table or view in PostgreSQL database and need to
I have an existing database (PostgreSQL in my case), and would like to access
I have a PostgreSQL database at work that is a single table of data.
I have a movie database (postgreSQL). One of tables contains actors and movie titles.
I have an article table on a Postgresql 9.1 database and a trigger that
I have a postgresql database I would like to convert to UTF-8. The problem
I have a table in my Postgres database with columns named type, desc, and
I have a postgres database with a user table (userid, firstname, lastname) and a

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.