+ -
当前位置:首页 → 问答吧 → 求一条查询语句 郁闷中...

求一条查询语句 郁闷中...

时间:2011-11-29

来源:互联网

1.表结构如下:
start_date_time stop_date_time
2002-01-28 14:55:00 2002-02-07 13:44:26
2002-01-28 14:55:00 2002-02-09 9:53:00
2002-01-28 14:55:00 2002-02-09 9:53:00
2002-01-28 14:55:00 null
2002-01-28 14:55:00 2002-02-09 9:53:00
2002-01-28 14:55:00 2002-02-09 9:53:00
2002-01-28 14:55:00 2002-02-09 9:53:00
2002-01-28 14:55:00 2002-02-09 9:53:00

2.查询语句 :
传入参数:例如 2001-01-30 只传入当天的日期不包括时间,
要求:1.查询出 start_date_time < 传入参数(2001-01-30 )> stop_date_time 数据
  如果:stop_date_time 为空时,只考虑条件 start_date_time < 传入参数(2001-01-30 )

请大家帮忙分析这个查询语句怎么写  
 

作者: david_anwei   发布时间: 2011-11-29

查询结果中 不包括 start_date_time stop_date_time 的日期

作者: david_anwei   发布时间: 2011-11-29

start_date_time T and (T>stop_date_time or stop_date_time is null)

作者: scrack   发布时间: 2011-11-29

SQL code
start_date_time < 传入参数(2001-01-30> stop_date_time 数据
stop_date_time 为空时,只考虑条件 start_date_time < 传入参数(2001-01-30select * from table1
where case when stop_date_time is null then
trunc(start_date_time,'mm')<trunc('2000-01-30','mm') and trunc(stop_date_time,'mm')<trunc('2000-01-30','mm') else  trunc(start_date_time,'mm')<trunc('2000-01-30','mm') end 

作者: cosio   发布时间: 2011-11-29

把你要的结果列出来

作者: programmerxiaocai   发布时间: 2011-11-29

SQL code

select *
from tb 
where (stop_date_time is null or stop_date_time<to_date('2001-01-30','yyyy-mm-dd')) 
and start_date_time<to_date('2001-01-30','yyyy-mm-dd')

作者: lxpbs8851   发布时间: 2011-11-29

SQL code
select *from tb 
where case when stop_date_time is null then start_date_time<to_date('2001-01-30','yyyy-mm-dd') 
else stop_date_time<to_date('2001-01-30','yyyy-mm-dd') end

作者: xpingping   发布时间: 2011-11-29

cosio 你好!有个地方报错 请教一下
select * from ORDERS 
where case when stop_date_time is null then trunc(start_date_time,'yyyy-mm-dd') < trunc('2000-01-30','yyyy-mm-dd') else trunc(start_date_time,'yyyy-mm-dd') < trunc('2000-01-30','yyyy-mm-dd') and trunc(stop_date_time,'yyyy-mm-dd') < trunc('2000-01-30','yyyy-mm-dd') end 

红色标示处 MISSING KEYWORD

作者: david_anwei   发布时间: 2011-11-29

转换后的日期存在问题
  select start_date_time,to_date(start_date_time,'yyyy-mm-dd') from ORDERS

查询结果: 
1 2002-01-28 14:55:00 0028-01-02
2 2002-01-28 14:55:00 0028-01-02
3 2002-01-28 14:55:00 0028-01-02
4 2002-01-28 14:55:00 0028-01-02
5 2002-01-28 14:55:00 0028-01-02

作者: david_anwei   发布时间: 2011-11-29