我有一个表,该表具有一系列基于时间的事件,每个事件都有开始和结束日期。对于最新(当前)事件,结束日期为NULL。我试图折叠重复的行,只显示最早的开始日期和最新的结束日期。在日期字段中为NULL的情况下,该行将被忽略。我可以使用NVL()虚设结束日期值,但这将导致前端逻辑搜索并替换该值。
无论如何,有没有要获得max()函数来对NULL进行高排序?
CREATE TABLE CONG_MEMBER_TERM ( CONG_MEMBER_TERM_ID NUMBER(10) NOT NULL, CHAMBER_CD VARCHAR2(30 BYTE) NOT NULL, CONG_MEMBER_ID NUMBER(10) NOT NULL, STATE_CD CHAR(2 BYTE) NOT NULL, DISTRICT NUMBER(10), START_DT TIMESTAMP(6) WITH TIME ZONE, END_DT TIMESTAMP(6) WITH TIME ZONE )
该查询有效,但是删除结束日期为NULL的行。
select CONG_MEMBER_ID, district, min(start_dt), max(end_dt) from CONG_MEMBER_TERM where CONG_MEMBER_ID = 1716 group by CONG_MEMBER_ID, district;
该查询可解决此问题,但现在我有一个“虚拟”结束日期值(9/9/9999)。我宁愿不必编写代码。
select CONG_MEMBER_ID, district, min(start_dt), max(nvl(end_dt, to_date('9/9/9999', 'mm/dd/yyyy'))) from CONG_MEMBER_TERM where CONG_MEMBER_ID = 1716 group by CONG_MEMBER_ID, district;
谢谢。
max(end_dt) keep (dense_rank first order by end_dt desc nulls first)
更新:
SQL
Oracle 11g R2架构设置 :
CREATE TABLE t (val int, s date, e date) ; INSERT ALL INTO t (val, s, e) VALUES (1, sysdate-3, sysdate-2) INTO t (val, s, e) VALUES (1, sysdate-2, sysdate-1) INTO t (val, s, e) VALUES (1, sysdate-1, null) INTO t (val, s, e) VALUES (2, sysdate-1, sysdate-.5) INTO t (val, s, e) VALUES (2, sysdate-.5, sysdate-.25) SELECT * FROM dual ;
查询1 :
select val, min(s), max(e) keep (dense_rank first order by e desc nulls first) from t group by val
结果 :
| VAL | MIN(S) | MAX(E)KEEP(DENSE_RANKFIRSTORDERBYEDESCNULLSFIRST) | --------------------------------------------------------------------------------------------- | 1 | November, 13 2012 14:15:46+0000 | (null) | | 2 | November, 15 2012 14:15:46+0000 | November, 16 2012 08:15:46+0000 |