选择之前的Jop by date-1 SQL / Crystal报告

时间:2015-12-31 11:29:22

标签: oracle crystal-reports

我有一个查询来从Table1中选择数据

EMP_NO   NAME_EMP   VALID_FROM     VALID_TO        Jop_NAME
132001   Joun       01/01/2015     31/12/9999      HR_ADMINSTRATOR
132001   Joun       15/03/2013     31/12/2014      HR_EMP
132001   Joun       11/2/2010      14/03/2013      DATA_ENTRY

我需要结果像这样

EMP_NO  NAME_EMP    VALID_FROM  VALID_TO    Jop_NAME        Jop1    B_Job_Date
132001  Joun        01/01/2015  31/12/9999  HR_ADMINSTRATOR HR_EMP  31/12/2014
132001  Joun        15/3/2013   31/12/2014  HR_EMP          HR_EMP

它运行正常但是当它放在Crystal Report中时我遇到了查询引擎错误:

  

ORA-01847:每月的日期必须介于1月和最后一天之间   原因日期-1>>>>>> aa.valid_from-1 B_Job_Date

这是我的选择:

    select a.EMP_NO,a.NAME_EMP,a.valid_from,a.VALID_TO,a.Jop_NAME ,h.Jop1,h.B_Job_Date
 from Table1 a, ( select aa.EMP_NO,aa.VALID_FROM,aa.VALID_TO,aa.valid_from-1
 B_Job_Date,Jop_NAME Jop1 from Table1 aa where aa.emp_no Like '&EMP_NO' and 
aa.valid_to ='31/12/9999')h where a.emp_no like '&EMP_NO' and a.emp_no=h.EMP_NO 
and ((h.B_Job_Date = a.valid_to) or (a.valid_to ='31/12/9999')) 

1 个答案:

答案 0 :(得分:0)

尝试转换为正确的日期格式,然后触发查询。

    select a.EMP_NO,a.NAME_EMP,a.valid_from,a.VALID_TO,a.Jop_NAME ,h.Jop1,h.B_Job_Date
     from Table1 a, ( select aa.EMP_NO,aa.VALID_FROM,TO_DATE
(aa.VALID_TO, 'mm/dd/yy'), TO_DATE(aa.valid_from-1, 'mm/dd/yy')
     B_Job_Date,Jop_NAME Jop1 from Table1 aa where aa.emp_no Like '&EMP_NO' and 
    TO_DATE(aa.valid_to, 'mm/dd/yy') =TO_DATE('31/12/9999', 'mm/dd/yy'))h where
 a.emp_no like '&EMP_NO' and a.emp_no=h.EMP_NO 
    and ((h.B_Job_Date = a.valid_to) or (a.valid_to ='31/12/9999'))