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?
There you go:
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: