在SQL查询中,effective_end_date不是31-dec-4712

时间:2016-03-21 19:22:27

标签: sql oracle

我有一个表tab_assignment_xx

date_from   date_end     action  person_number
01-Apr-2014 31-Jul-2014   HIRE   050498
01-Aug-2014 31-Jan-2015   OTHERS    050498
01-Feb-2015 30-Jun-2015   OTHERS    050498
01-Jul-2015 15-Nov-2015   OTHERS    050498
16-Nov-2015 31-Dec-2015 OTHERS  050498
01-Jan-2016 30-JAN-2016 OTHERS  050498

01-APR-2016  31-JUL-2016   hire    83982
01-jan-2015  31-dec-4712   Others   6447

现在我想检查所有那些具有effective_start_date和effective_End_date的特定员工,例如它应该具有31-dec-4712 max(effective_end_date):对于050498,max(effective_end_date)不是'31 -dec-4712 '和83982一样.6447是正确的。

为此我做了:

  select * from (
    select T.*,
      max(EFFECTIVE_START_DATE) over (partition by PERSON_NUMBER order by EFFECTIVE_START_DATE) MAX_FROM
    from  tab_assignment_xx T
    where 1=1
    --T.PERSON_NUMBER = '093343'
    AND ASSIGNMENT_TYPE='E'
  )
  where MAX_FROM <> to_date('31-DEC-4712')
;

但是这不起作用,因为它是有和没有31-dec-4712 max eff结束日期的行

2 个答案:

答案 0 :(得分:0)

Oracle安装程序

CREATE TABLE table_name ( date_from, date_end, action, person_number ) AS
SELECT DATE '2014-04-01', DATE '2014-07-31', 'HIRE',   '050498' FROM DUAL UNION ALL
SELECT DATE '2014-08-01', DATE '2015-01-31', 'OTHERS', '050498' FROM DUAL UNION ALL
SELECT DATE '2015-02-01', DATE '2015-06-30', 'OTHERS', '050498' FROM DUAL UNION ALL
SELECT DATE '2015-07-01', DATE '2015-11-15', 'OTHERS', '050498' FROM DUAL UNION ALL
SELECT DATE '2015-11-16', DATE '2015-12-31', 'OTHERS', '050498' FROM DUAL UNION ALL
SELECT DATE '2016-01-01', DATE '2016-01-30', 'OTHERS', '050498' FROM DUAL UNION ALL
SELECT DATE '2016-04-01', DATE '2016-07-31', 'hire',   '83982'  FROM DUAL UNION ALL
SELECT DATE '2015-01-01', DATE '4712-12-31', 'Others', '6447'   FROM DUAL;

<强>查询

SELECT Date_From, Date_To, action, person_number
FROM   (
  SELECT t.*,
         MAX( date_end )
           OVER ( PARTITION BY person_number
                  ORDER BY ROWNUM
                  ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
                ) AS mx
  FROM   table_name t
)
WHERE mx <> DATE '4712-12-31';

<强>输出

DATE_FROM           DATE_END            ACTION PERSON_NUMBER
------------------- ------------------- ------ -------------
2014-04-01 00:00:00 2014-07-31 00:00:00 HIRE   050498        
2014-08-01 00:00:00 2015-01-31 00:00:00 OTHERS 050498        
2015-02-01 00:00:00 2015-06-30 00:00:00 OTHERS 050498        
2015-07-01 00:00:00 2015-11-15 00:00:00 OTHERS 050498        
2015-11-16 00:00:00 2015-12-31 00:00:00 OTHERS 050498        
2016-01-01 00:00:00 2016-01-30 00:00:00 OTHERS 050498        
2016-04-01 00:00:00 2016-07-31 00:00:00 hire   83982         

答案 1 :(得分:0)

请检查一下是否有效(我不试试)。评论是否有任何错误。

 select * from tab_assignment_xx a where to_date('31-DEC-4712')<>(select max(date_end) from tab_assignment_xx b where a.person_number=b.person_number);