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

david_anwei 2011-11-29 04:00:53
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 )

请大家帮忙分析这个查询语句怎么写
...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
david_anwei 2011-11-30
  • 打赏
  • 举报
回复
谁帮解答一下啊 !
dzntree 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 david_anwei 的回复:]

转换后的日期存在问题
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
……
[/Quote]

改成 to_char(start_date_time,'yyyy-mm-dd')
dzntree 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 david_anwei 的回复:]

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……
[/Quote]


case when 不能用在where里面的,你用楼上那个SQL
oO寒枫Oo 2011-11-30
  • 打赏
  • 举报
回复
2个字段是什么类型的。
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
  • 打赏
  • 举报
回复
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
xpingping 2011-11-29
  • 打赏
  • 举报
回复
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

oO寒枫Oo 2011-11-29
  • 打赏
  • 举报
回复

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')
programmerxiaocai 2011-11-29
  • 打赏
  • 举报
回复
把你要的结果列出来
cosio 2011-11-29
  • 打赏
  • 举报
回复
start_date_time < 传入参数(2001-01-30 )> stop_date_time 数据
stop_date_time 为空时,只考虑条件 start_date_time < 传入参数(2001-01-30 )



select * 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
scrack 2011-11-29
  • 打赏
  • 举报
回复
start_date_time T and (T>stop_date_time or stop_date_time is null)
david_anwei 2011-11-29
  • 打赏
  • 举报
回复
查询结果中 不包括 start_date_time stop_date_time 的日期

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧