大于当前日期范围的日期将被解码为空-SQL查询

时间:2019-02-05 20:05:07

标签: sql oracle

我创建了以下查询,以给我一个在01-Jan-2018 (c_date_from ) and 31-dec-2018 (c_date_to)开始的日期范围内的结果集

现在我必须对此进行更改。例如,如果我将c_date_from传递为2018年1月1日,并将c_date_to传递为2018年12月31日,请举例说明以下数据。如果结束日期是4712年12月31日,则将其视为空值。它适用于所有情况。 但是在代码的解码部分中,我现在想要以一种方式处理:如果assignment_end_date列的值大于c_date_to的值,它也应将其视为null。 例如:

+-----------+---------------+---------------+-----------------------+---------------------+--------+
| person_id | next_employee | employee_name | assignment_start_date | assignment_end_date | Client |
+-----------+---------------+---------------+-----------------------+---------------------+--------+
|         1 | XYZ           | GEN           | 02-JAN-2018           | 03-jun-18           | DEL    |
|         1 | REC           | GEN           | 04-jun-18             | 01-nov-18           | DEL    |
|         1 | YZ            | GEN           | 02-nov-18             | 01-JAN-2019         | DEL    |
|         2 | FDGD          | PA            | 01-JAN-2018           | 02-APR-2018         | FAR    |
|         2 | FDRY          | PA            | 03-APR-2018           | 31-DEC-4712         | FAR    |
+-----------+---------------+---------------+-----------------------+---------------------+--------+

对于上表,我的查询很好地获取了数据,并且当assignment_end_date为31-dec-4712时,它将在解码中将其更改为NULL。现在我想要的是,如果assignment_end_date(人的ID为1的最大日期为2019年1月1日)> c_date_to(在这种情况下为2018年12月31日),那么它将也视为nul l。我如何在同一查询的以下查询中处理它?<​​/ p>

 SELECT papf.person_id,
        LEAD(papf.employee_number) OVER( ORDER BY papf.employee_number, paaf.effective_start_date,change_date ) next_employee,
        PAPF.full_name employee_name,    
        to_char(paaf.effective_start_date,'DD-MON-YY') assignment_start_date, 
        DECODE(paaf.effective_end_date, to_date('12/31/4712', 'MM/DD/YYYY'), NULL, paaf.effective_end_date)  assignment_end_date,
        ppd.segment3 Client,       
            from 
                xl.x_current_employees_gtt PAPF , -
                per_all_assignments_f PAAF,
                per_pay_proposals ppp,
                per_position_definitions ppd             
            WHERE 1 = 1
             --AND papf.employee_number in (60180)
                AND ppp.change_date(+) <= c_date_to  
             AND ppp.date_to(+)     >= c_date_from 
             AND ppp.change_date(+) <= paaf.effective_end_date
             AND ppp.date_to(+)     >= paaf.effective_start_date
             AND ppp.assignment_id(+) = paaf.assignment_id
             AND paaf.primary_flag = 'Y'
             AND paaf.assignment_status_type_id = g_assignment_status_type_id
             AND paaf.assignment_type = 'E'
             AND paaf.effective_start_date <= paaf.effective_end_date -- There are some cases, due to which this condition has been included
             AND paaf.effective_start_date <= c_date_to 
             AND paaf.effective_end_date   >= c_date_from 
             AND PAAF.person_id IN ( PAPF.person_id , NVL(papf.sez_person_id,PAPF.person_id) )
            ORDER BY employee_id,paaf.effective_start_date , change_date ; 

1 个答案:

答案 0 :(得分:2)

目前尚不清楚c_date_to的来源,但我认为它是这些表之一中的一列。因此,我将DECODE转换为CASE

CASE WHEN paaf.effective_end_date = DATE '4712-12-31` OR 
     paaf.effective_end_date > c_date_too THEN NULL 
     ELSE paaf.effective_end_date END AS assignment_end_date

我发现使用DATE文字比TO_DATE短一些,可读性强。