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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:31:28+00:00 2026-06-17T12:31:28+00:00

Sql problem: determine an event’s active period from start date and end date split

  • 0

Sql problem: determine an event’s active period from start date and end date split in 2 tables

This is an adhoc report that I need to produce. I was provided 2 extract files pertaining to events (csv). I intend to insert these extracts to a single sql table #temp_events.

File 1:

event, start date
Core training, 01Jan2011
Certification reviews, 23Feb2011
Core training, 15May2011
Abc Event, 24Nov2011

File 2:

event, end date
Core training, 05Jan2011
Certification reviews, 24Feb2011
Core training, 18May2011
Abc Event, 30Nov2011

I want to insert into a #temp_events, wherein an entry will define an event’s active period. Note that there is no FK, as this is the primary problem at hand… the only way to relate data is via event name however there could be multiple occurrences of an event. An event’s end cannot be calculated since there is no data that defines how long an event is. These are the restrictions that I need to deal with as extract provider has stipulated, unfortunately.

Can anyone provide suggestions for the logic that I need to code, so that I can reliably link event’s in File 1 to File 2 even though there is no natural key/id/FK? Any obvious loophole that I may run into?

  • 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-17T12:31:29+00:00Added an answer on June 17, 2026 at 12:31 pm

    There you go:

    CREATE TABLE eventstart
            ( event VARCHAR NOT NULL
            , zdate date
            );
    INSERT INTO eventstart(event,zdate) VALUES
     ( 'Core training' , '2011-01-01' )
    ,( 'Certification reviews' , '2011-02-23' )
    ,( 'Core training' , '2011-05-15' )
    ,( 'Abc Event' , '2011-11-24' )
            ;
    
    
    CREATE TABLE eventend
            ( event VARCHAR NOT NULL
            , zdate date
            );
    INSERT INTO eventend(event,zdate) VALUES
     ('Core training' , '2011-01-05' )
    ,('Certification reviews' , '2011-02-24' )
    ,('Core training' , '2011-05-18' )
    ,('Abc Event' , '2011-11-30' )
            ;
    
    WITH    s AS (
            SELECT ss.event, ss.zdate
            , ROW_NUMBER() OVER( PARTITION BY ss.event ORDER BY ss.zdate) AS zrnk
            FROM eventstart ss
            )
            ,e AS (
            SELECT ee.event, ee.zdate
            , ROW_NUMBER() OVER( PARTITION BY ee.event ORDER BY ee.zdate) AS zrnk
            FROM eventend ee
            )
    SELECT s.event
           , s.zdate AS sdate
           , e.zdate AS edate
    FROM s
    LEFT JOIN e ON e.event = s.event AND s.zrnk = e.zrnk
    WHERE s.zdate <= COALESCE(e.zdate , s.zdate) -- safety valve ;-)
            ;
    

    Note: the above query is very rude. It relies on the borderconditions given by the OP, that there will be a matching end-record for every start record. If not: YMMV

    Result:

    CREATE TABLE
    INSERT 0 4
    CREATE TABLE
    INSERT 0 4
             event         |   sdate    |   edate    
    -----------------------+------------+------------
     Abc Event             | 2011-11-24 | 2011-11-30
     Certification reviews | 2011-02-23 | 2011-02-24
     Core training         | 2011-01-01 | 2011-01-05
     Core training         | 2011-05-15 | 2011-05-18
    (4 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 5 tables in MySQL, but i believe this is a SQL problem
This problem is similar to blind SQL injections. The goal is to determine the
another sql problem of mine .. this time the reader doesn't work properly (
This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL)
I have used LogCat to determine the problem line. The SQL String I am
I have a problem in a SQL query. There are two tables: 1: POrder
In Oracle SQL Developer, how does one determine the calendar date of the next
I have an interesting SQL problem that I need help with. Here is the
I have a complex SQL problem in MS SQL Server, and in drawing on
I am having an argument with a developer colleague on the team. Problem: SQL

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.