在Oracle SQLDeveloper中,我尝试运行查询,其中我发现返回特定日期作为StartDate,但如果最早的现有日期大于指定的开始日期,则返回最小日期。我尝试使用CASE语句执行此操作,但我不断收到“缺少正确的括号”错误。到目前为止,这是我的代码:
select
a.program
, a.match_file
, (case when b.date = '27-JAN-13' then b.date else min(b.date end)) as Startdate
from Schema1.program a, Schema1.detail b
where a.match_file = b.matchfile
and (case when b.date=' ' then min(b.date) else b.date end) = '27-JAN-13'
group by a.program, a.match_file
;
有人可以告诉我,如果我错了吗?我也尝试过使用IF语句和几个不同的UNION语句,但我无法让它返回我想要的内容。
答案 0 :(得分:3)
您可能需要考虑COALESCE函数(选择第一个非null参数)。但是,如果要过滤空白而不是空值,则可能无效。在这种情况下,CASE声明应该适合您的需要。
在你的代码中,"结束"在你的第一个case语句中是在min()内,这可能是你得到错误的原因。
, (case when b.date = '27-JAN-13' then b.date else min(b.date) end) as Startdate
我无法弄清楚你要做什么,但根据你的描述我已经提出了下面的sql。我不认为你需要在where子句中进行任何过滤,因为你想要每一行结果。您只想知道每个程序/文件组合,如果最早的日期是1月27日之后。 select中的case语句负责这一点。
"返回特定日期作为StartDate,但返回最小日期 如果最早的现有日期大于指定的开始日期 日期"
select
a.program
, a.match_file
, case when min(b.date) > '27-JAN-13' then min(b.date) else '27-JAN-13' end as Startdate
from Schema1.program a, Schema1.detail b
where a.match_file = b.matchfile
group by a.program, a.match_file;
答案 1 :(得分:1)
您的陈述在语法上是不正确的。
将end
移动到最小聚合之外,如下所示
select
a.program
, a.match_file
, (case when b.date = '27-JAN-13' then b.date else min(b.date) end) as Startdate
from Schema1.program a, Schema1.detail b
where a.match_file = b.matchfile
and (case when b.date=' ' then min(b.date) else b.date end) = '27-JAN-13'
group by a.program, a.match_file
;