没有联合的查询重构
Query Refactor without union
我们可以在不使用 union 的情况下重构以下查询吗?
也许我们可以在 where 条件下使用 or 逻辑运算符。
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
您不需要任何逻辑运算符,从 where
子句中删除 REM_MAIL_SENT_DTTS
条件
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
我认为这可以重写为:
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;
我们可以在不使用 union 的情况下重构以下查询吗? 也许我们可以在 where 条件下使用 or 逻辑运算符。
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
您不需要任何逻辑运算符,从 where
子句中删除 REM_MAIL_SENT_DTTS
条件
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
我认为这可以重写为:
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;