简体   繁体   English

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

[英]Date greater than current date range to be decoded to null- SQL Query

I have created the below query to give me a result set within a date range say from 01-Jan-2018 (c_date_from ) and 31-dec-2018 (c_date_to) 我创建了以下查询,以便在日期范围内(例如01-Jan-2018 (c_date_from ) and 31-dec-2018 (c_date_to)提供给我一个结果集

Now I have to make a change in this. 现在,我必须对此进行更改。 Say for example for the below data if I pass c_date_from as 01-Jan-2018 and c_date_to as 31-dec-2018. 例如,如果我将c_date_from传递为2018年1月1日,并将c_date_to传递为2018年12月31日,请举例说明以下数据。 If the end date is 31-Dec-4712 it will treat it as null. 如果结束日期是4712年12月31日,则将其视为空值。 It is working fine for all cases. 它适用于所有情况。 But in the decode part of the code I now want to handle in such a way that if assignment_end_date column is > than the c_date_to it should treat it as null as well. 但是在代码的解码部分中,我现在想以一种方式处理,如果assignment_end_date列的值大于c_date_to的值,它也应将其视为null。 For example : 例如 :

+-----------+---------------+---------------+-----------------------+---------------------+--------+
| 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    |
+-----------+---------------+---------------+-----------------------+---------------------+--------+

For the above table, my query fetches the data well and when the assignment_end_date is 31-dec-4712 then it changes it to NULL in the decode. 对于上表, 我的查询很好地获取了数据,当assignment_end_date为31-dec-4712时,它将在解码中将其更改为NULL。 Now i want if the assignment_end_date(person id 1 max date 01-jan-2019) is > c_date_to (31-dec-2018 in this case) then also it treats it as nul l. 现在我想要的是,如果assignment_end_date(人的ID为1的最大日期为2019年1月1日)> c_date_to(在这种情况下为2018年12月31日),那么它将也视为nul l。 How can i handle it in the below query in the same de?ode. 我如何在同一查询的以下查询中处理它?

 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 ; 

It's not clear where c_date_to is coming from, but I assume it's a column in one of these tables. 目前尚不清楚c_date_to的来源,但我认为它是这些表之一中的一列。 So I would convert the DECODE to a CASE : 所以我将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

I find using a DATE literal a bit shorter and more readable than TO_DATE . 我发现使用DATE文字比TO_DATE短并且可读性强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM