【发布时间】:2019-03-16 04:15:52
【问题描述】:
我有以下要求,但我不知道如何编写 Oracle SQL 查询以根据给定条件获取数据。
要求:
人员表
Name created_date updated_date
------- ----------- ------------
Alex 11-oct-2018
John 10-oct-2018 11-oct-2018
我想根据 UI 上给出的 from 和 to date 来获取记录名称 created_date 或 updated_date 作为 last_modified_date,如果 updated_date 不为 null,它应该在 fromdate 和 todate 之间的 updated_date 上搜索 updated_date。如果updated_date 为null,那么它应该在fromdate 和todate 之间搜索created_date,如created_date。
我这样试过,有编译问题,我不怎么写
select name,
case when update_date is null then created_date else updated_date as last_modified_date
from Person
where case when updated_date is null
then trunc(created_date) between fromdate and todate
else trunc(updated_date) between fromdate and todate.'
【问题讨论】:
-
通常最好在 WHERE 子句中使用 AND/OR 结构,而不是 case/coalesce 等。
-
你能提供样本输出吗??
-
非常感谢您的帮助。
-
这里列出的所有答案都很完美。感谢您的帮助。