查询没有联合的重构

时间:2015-03-16 13:59:13

标签: sql oracle union

我们可以在不使用union的情况下重新考虑低于查询的因素。 可能是我们可以在where where条件下使用或逻辑运算符。

select a.RequestNumber, a.ticket_id as TicketID, a.requester,   num_business_days (a.last_modified_date, sysdate) as businessdays,a.REM_MAIL_SENT_DTTS from (SELECT ticket_id, request_number as  RequestNumber, created_by as requester,
  last_modified_date,REM_MAIL_SENT_DTTS

      From Ri_Ticket_Info
      Where Status_Id = 6 And REM_MAIL_SENT_DTTS   Is Null)A
      where num_business_days (a.last_modified_date, sysdate) >= 5

      union

      select a.RequestNumber, a.ticket_id as TicketID, a.requester,   num_business_days (a.last_modified_date, sysdate) as businessdays,a.REM_MAIL_SENT_DTTS from (SELECT ticket_id, request_number as  RequestNumber, created_by as requester, 
      last_modified_date,REM_MAIL_SENT_DTTS

      From Ri_Ticket_Info
      Where Status_Id = 6 And REM_MAIL_SENT_DTTS   Is not Null  )A
      where num_business_days (a.REM_MAIL_SENT_DTTS, sysdate) >= 5  

2 个答案:

答案 0 :(得分:2)

我认为这可以改写为:

SELECT a.requestnumber,
       a.ticket_id AS ticketid,
       a.requester,
       num_business_days (a.last_modified_date, SYSDATE) AS businessdays,
       a.rem_mail_sent_dtts
FROM   ri_ticket_info a
WHERE  a.status_id = 6
and    num_business_days (nvl(a.rem_mail_sent_dtts, a.last_modified_date), SYSDATE) >= 5;

答案 1 :(得分:1)

您不需要任何逻辑运算符,从REM_MAIL_SENT_DTTS子句

中删除where条件
SELECT a.RequestNumber,
       a.ticket_id                                       AS TicketID,
       a.requester,
       Num_business_days (a.last_modified_date, sysdate) AS businessdays,
       a.REM_MAIL_SENT_DTTS
FROM   (SELECT ticket_id,
               request_number AS RequestNumber,
               created_by     AS requester,
               last_modified_date,
               REM_MAIL_SENT_DTTS
        FROM   Ri_Ticket_Info
        WHERE  Status_Id = 6)A
WHERE  Num_business_days (a.last_modified_date, sysdate) >= 5