I'm using DB2, although a solution using any flavor of SQL would likely be easy enough for me to convert.
I didn't design this database, or the application that uses the database. I haven't the power to change this application, how it works, or the data. Because it defies what I consider to be conventional use of a start and an end date, I am struggling with writing something as simple as a select for a specific point in time.
Here are the relevant/edited parts of the table:
OBJECTID FACILITY_ID START_DATE END_DATE FACILITY_NAME
1001 500 1/1/1980 5/1/2000 Really Old Name
1002 500 1/1/1980 1/1/2006 Old Name
1003 500 1/1/1980 null Current Name
1004 501 1/1/1980 3/1/2008 Closed Facility Name
1004 502 1/1/1980 null Another Current Name
What I want to return, are the records which are valid for 7/1/2005:
OBJECTID FACILITY_ID START_DATE END_DATE FACILITY_NAME
1002 500 1/1/1980 1/1/2006 Old Name
1004 501 1/1/1980 3/1/2008 Closed Facility Name
1004 502 1/1/1980 null Another Current Name
I'm trying to avoid subselects, but understand they may be necessary. If I do need a subselect, I'd like to keep it limited to one. Looking between the start and end date doesn't work, because it doesn't return facilities which have only one record with a null end date. Adding an OR condition to include end dates which are null may return more than one record in some cases. This problem seems so simple on the service, that I must be missing a ridiculously obvious solution. Does anyone have any ideas?