选择日期范围内的所有日期范围

时间:2018-03-22 09:20:42

标签: sql oracle oracle11g

let subject = new Subject<string>();
let wrapper = new Wrapper(subject);
wrapper.wrapped$.subscribe();
subject.next("foo")

我想要的是选择创建日期和输入的start_range和end_range之间的到期日期之间的值。

例如

_______________________________________
|item    |created       |expiry       |
_______________________________________
|A       |01/01/2000    |01/02/2000   |
|B       |01/04/2000    |01/06/2000   |
|C       |01/05/2000    |01/11/2000   |
|D       |01/02/2000    |01/05/2000   |
|E       |01/06/2000    |01/07/2000   |

上面的范围将具有值

start_range: 01/03/2000
end_range:   01/05/2000

输出如下:

01/03/2000
01/04/2000 
01/05/2000

不包括A和E,因为它们之间的日期不在开始和结束范围之间的日期。

3 个答案:

答案 0 :(得分:3)

SELECT item,TO_CHAR(created,'DD/MM/YYYY') created,TO_CHAR(expiry,'DD/MM/YYYY') expiry
FROM Table1
WHERE created <= DATE '2000-01-05' AND expiry >= DATE '2000-01-03';

输出

ITEM    CREATED EXPIRY
B   04/01/2000  06/01/2000
C   05/01/2000  11/01/2000
D   02/01/2000  05/01/2000

现场演示

  

http://sqlfiddle.com/#!4/bee9f/16

答案 1 :(得分:1)

将人类语言直接翻译为您想要的sql:

  

选择创建日期与输入的start_range和end_range之间的到期日期之间的所有值

select item, created, expiry
from table
where created between start_range and end_range
   and expiry between start_range and end_range;

答案 2 :(得分:1)

以下是您问题的解决方案:

SELECT Item, created, expiry
FROM Table1
WHERE 
created BETWEEN DATE '2000-01-03' AND DATE '2000-01-05'
OR
expiry BETWEEN DATE '2000-01-03' AND DATE '2000-01-05';

点击演示链接:

  

http://sqlfiddle.com/#!4/bee9f/2

另一种方式:

SELECT Item, created, expiry
FROM Table1
WHERE created <= DATE '2000-01-05' AND expiry >= DATE '2000-01-03';

一般查询:

SELECT Item, created, expiry
    FROM Table1
    WHERE created <= DATE end_range AND expiry >= DATE start_range;

点击演示链接:

  

http://sqlfiddle.com/#!4/bee9f/7