如何编写查询以查找以下数据空白。
- Scenario1 - Query1 - 找到同一表内的差距(例如tableb)
ID EFF_DT TERM_DT
800 6/15/2012 12:00:00.000 AM 10/14/2012 12:00:00.000 AM -- GAP
800 10/16/2012 12:00:00.000 AM 11/14/2012 12:00:00.000 AM
800 11/15/2012 12:00:00.000 AM 12/31/9999 12:00:00.000 AM
- Scenario2 - Query2 - 找出2个表(tablea,tableb)之间的不匹配
-- tablea , sample data below
ID EFF_DT TERM_DT
400 6/15/2012 12:00:00.000 AM 10/14/2012 12:00:00.000 AM
400 11/15/2012 12:00:00.000 AM 12/31/9999 12:00:00.000 AM
500 6/15/2012 12:00:00.000 AM 10/14/2012 12:00:00.000 AM
500 10/15/2012 12:00:00.000 AM 11/14/2012 12:00:00.000 AM
500 11/15/2012 12:00:00.000 AM 12/31/9999 12:00:00.000 AM
-- tableb, sample data below
ID EFF_DT TERM_DT
400 6/15/2012 12:00:00.000 AM 10/13/2012 12:00:00.000 AM -- mismatch
400 11/15/2012 12:00:00.000 AM 12/31/9999 12:00:00.000 AM
500 6/15/2012 12:00:00.000 AM 11/14/2012 12:00:00.000 AM
500 11/15/2012 12:00:00.000 AM 12/31/9999 12:00:00.000 AM
-- Note 400 is a mismatch since ranges are different in both tables, 500 is okay (even though rows are split they still match )
-- Need to identify ID 400
对于低日期,表格有时为1/1/1753而对于高日期有时为12/31/9999。
尝试了以下查询,但它会在第一个方案中抛出oracle错误。 [ORA-01841 :(完整)年份必须介于-4713和+9999之间,而不是0]
SELECT ID, TERM_DT AS gap_lower_bound, next_date AS gap_upper_bound
FROM (SELECT t.*,
LEAD (EFF_DT)
OVER (PARTITION BY ID ORDER BY EFF_DT)
next_date
FROM TABLEB t)
WHERE -- trim(next_date) is not null and
next_date > TERM_DT + 1
谢谢。
答案 0 :(得分:2)
for Scenario1 ,, 让我们来看一个样本,
ID EFF_DT TERM_DT
____________________________________
800 15-JUN-12 14-OCT-12 --GAP
800 16-OCT-12 14-NOV-12
800 15-NOV-12 31-DEC-12
800 01-JAN-13 01-FEB-13 --GAP
800 03-FEB-13 01-MAR-13
查询可以是
SELECT term_dt
FROM
(SELECT t.*,
lead(eff_dt,1) OVER (ORDER BY eff_dt) AS next_date
FROM lead_test t
)
WHERE term_dt+1<>next_date;
结果:
term_dt
__________
14-OCT-12
01-FEB-13
for scenario2:
由于select to_date('12/31/9999', 'mm/dd/yyyy')+1 from dual;
会抛出相同的错误。
由于您在ORA-01841
中使用的where子句中的条件,导致TERM_DT + 1
错误,因此您应该更改/减少12/31/9999
中的值。