我遇到类似于在delete_reason_!='deleted'时读取max(end_time_)的情况,但是当end_time_为null时,查询应仅返回第2行。
SELECT MAX(END_TIME_) FROM TASK_HISTORY WHERE DELETE_REASON_ != 'deleted'
回到第一排。但是,我想要的结果应该返回第二行。根据Oracle文档,max,sum,min等聚合函数不应该考虑与Count不同的空值。
如果出现null,是否有办法将Null值设为max;否则,max(end_time_)应该是我想要的输出。
任何帮助将不胜感激。
谢谢。答案 0 :(得分:0)
如果我理解正确,这里有一个选项:
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> with test (end_time_, delete_reason_) as
2 (select date '2017-12-13', 'completed' from dual union
3 select null, null from dual union
4 select date '2017-12-12', 'deleted' from dual union
5 select date '2018-01-05', 'unknown' from dual
6 ),
7 inter as
8 (select row_number() over (order by end_time_ desc) rn,
9 end_time_, delete_reason_
10 from test
11 where nvl(delete_reason_, 'x') <> 'deleted'
12 )
13 select end_time_ From inter
14 where rn = 1;
END_TIME_
----------
NULL
SQL>
SQL> with test (end_time_, delete_reason_) as
2 (select date '2017-12-13', 'completed' from dual union
3 -- select null, null from dual union
4 select date '2017-12-12', 'deleted' from dual union
5 select date '2018-01-05', 'unknown' from dual
6 ),
7 inter as
8 (select row_number() over (order by end_time_ desc) rn,
9 end_time_, delete_reason_
10 from test
11 where nvl(delete_reason_, 'x') <> 'deleted'
12 )
13 select end_time_ From inter
14 where rn = 1;
END_TIME_
----------
05.01.2018
SQL>
答案 1 :(得分:0)
最后,当我使用max()函数时,我得到了一个像这样的查询来实现null值而不是date。
SELECT task_def_key_
, CASE WHEN MAX(CASE WHEN end_time_ IS NULL THEN 1 ELSE 0 END) = 0 THEN MAX(end_time_) END
FROM TASK_HISTORY WHERE DELETE_REASON_ != 'deleted'
GROUP BY task_def_key_
答案 2 :(得分:0)
从*中选择*(从TASK_HISTORY中选择*,其中DELETE_REASON_!=“由end_time desc删除”订单),其中rownum = 1
这将解决您的问题