2

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?

4

6 回答 6

2

would this work:

SELECT * FROM table_name
WHERE START_DATE < '7/1/2005' AND (END_DATE > '7/1/2005' OR END_DATE IS NULL);
于 2008-10-08T16:40:10.490 回答
1

诀窍是将结束日期合并到第二天,:) Coalesce 基本上用第二个参数替换空值。很酷的小技巧。

select * from TAble where START_DATE < @DATE and Coalesce(END_DATE, @DATE+1) > @DATE
于 2008-10-08T16:40:57.133 回答
1

取2:

select OBJECTID, FACILITY_ID, START_DATE, FACILITY_NAME, MIN(END_DATE) as  END_DATE
from facitities
where START_DATE <= @my_date and (@mydate <= END_DATE or END_DATE is null)
group by OBJECTID, FACILITY_ID, START_DATE, FACILITY_NAME
于 2008-10-08T16:51:41.717 回答
1

很抱歉回答我自己的问题。谢谢大家,给出的答案非常有帮助。准确地思考我想要完成的工作也很有帮助。结合给出的答案中的概念,我能够想出一些似乎可行的方法:

SELECT 
    * 
FROM 
    FACILITY_TABLE
WHERE 
    (END_DATE IS NULL
     AND OBJECTID NOT IN 
     (SELECT A.OBJECTID FROM FACILITY_TABLE A 
     WHERE '7/1/2005' BETWEEN A.BEGINDATE AND A.ENDDATE))
  OR 
    '7/1/2005' BETWEEN FACILITY_TABLE.START_DATE AND FACILITY_TABLE.ENDDATE

由于数据使开始日期变得毫无意义,我没有包括它。如果记录从现在到那时过期,则仅返回 2005 年 7 月 1 日有效的记录,而不包括当前记录。

于 2008-10-08T18:49:22.913 回答
0

从你给我们的来看,这就是你想要的:

select * from facitities
where START_DATE <= @my_date and (@mydate <= END_DATE or END_DATE is null)

但是,我怀疑您知道这一点,并且想要一些不同的东西,在这种情况下,您必须更具体地了解数据的问题。

于 2008-10-08T16:43:58.953 回答
0

以上略有不同:

select * from facilities
where @my_date between START_DATE AND COALESCE(END_DATE, CURRENT DATE)
于 2008-12-24T15:41:13.647 回答