我有一个包含一系列基于时间的事件的表,每个事件都有一个开始和结束日期。对于最近(当前)事件,结束日期为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;
感谢。
答案 0 :(得分:23)
max(end_dt) keep (dense_rank first order by end_dt desc nulls first)
<强> UPD:强>
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
<强> Results 强>:
| 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 |
答案 1 :(得分:1)
select CONG_MEMBER_ID
, district
, min(start_dt)
, NULLIF(MAX(NVL(end_dt
,TO_DATE('9999-09-09','YYYY-MM-DD')
)
)
,TO_DATE('9999-09-09','YYYY-MM-DD')
)
from CONG_MEMBER_TERM
where CONG_MEMBER_ID = 1716
group by CONG_MEMBER_ID
, district
;