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

  • Home
  • SEARCH
  • 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 6470717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:08:34+00:00 2026-05-25T06:08:34+00:00

I have Work and Person tables (these are just examples to understand problem). Structure

  • 0

I have Work and Person tables (these are just examples to understand problem).

Structure

Work table

id INTEGER
person_id INTEGER
dt_from DATETIME
dt_to DATETIME

Person table

person_id INTEGER
name VARCHAR(50)

Data

Work table

id | person_id | dt_from    | dt_to
-------------------------------------------------
1  | 1         | 2011-01-01 | 2011-02-02
2  | 1         | 2011-02-02 | 2011-04-04
3  | 1         | 2011-06-06 | 2011-09-09
4  | 2         | 2011-01-01 | 2011-02-02
5  | 2         | 2011-02-02 | 2011-03-03
....etc.

Person table

Just person names with person id

Expected output

Person 1 : 2011-01-01 - 2011-04-04
Person 1 : 2011-06-06 - 2011-09-09
Person 2 : 2011-01-01 - 2011-03-03

Interval must be in sequence. It cannot be broken somewhere in middle. Thats why Person 1 have two intervals.

I’m using postgres if it changes something. Have you any thougths?
I wanted do it in one query, but if there is no such solution i will do some interval merge in php.

  • 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-25T06:08:34+00:00Added an answer on May 25, 2026 at 6:08 am

    There may be a way to do this in one SQL select, but it escapes me. I managed to do it with one stored function though. Here’s what I did for my testing:

    create table work
    (id integer, start_date date, end_date date);
    
    insert into work values (1, '2011-01-01','2011-02-02');
    insert into work values (1, '2011-02-02','2011-04-04');
    insert into work values (1, '2011-06-06','2011-09-09');
    insert into work values (2, '2011-01-01','2011-02-02');
    insert into work values (2, '2011-02-02','2011-03-03');
    
    create or replace function get_data() returns setof work as
    $body$
    declare
        res work%rowtype;
        sd  date := null;
    begin
        for res in
            select
                w1.id,
                case when exists (select 1 from work w2 where w1.id=w2.id and w2.end_date=w1.start_date) then null else w1.start_date end,
                case when exists (select 1 from work w2 where w1.id=w2.id and w2.start_date=w1.end_date) then null else w1.end_date end
            from
                work w1
            order by
                id, start_date, end_date
        loop
            if res.start_date is not null and res.end_date is not null then
                return next res;
            elsif res.start_date is not null then
                sd := res.start_date;
            elsif res.end_date is not null then
                res.start_date := sd;
                return next res;
            end if;
        end loop;
    
        return;
    end;$body$
      language 'plpgsql';
    

    Then

    select * from get_data() order by id, start_date;
    

    returned this result:

     id | start_date |  end_date
    ----+------------+------------
      1 | 2011-01-01 | 2011-04-04
      1 | 2011-06-06 | 2011-09-09
      2 | 2011-01-01 | 2011-03-03
    (3 rows)
    

    which is, I think, what you’re after.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables: CREATE TABLE person (id int, name char(50)) CREATE TABLE eventtype
I have a table that stores various clients I have done work for, separated
I have the following tables: Person, {Id, Name, LastName} Sports, {Id Name, Type} SportsPerPerson,
Just after some advice regarding this database design situation. So I have two tables
So I have three tables: Table Place: _id autoincrement not null primary key, name
Let's say I have a Person table and a Song table and a Person_Song
In Team Foundation Server is there a way to have work items in one
I have to work on an old 1.3 JVM and I'm asked to create
I have to work on several VB6 legacy projects and despite some good VB6
I have to work on some code that's using generic lists to store 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.