我有以下查询。 以下查询WITH CTE,它返回4条记录。PL SQL/Oracle中的这个CTE WITH子句有什么问题?
正如我给出的评论,下面的Select查询有55个记录和And子句它应该删除这4条记录并返回51条记录。相反,它仅返回那4条记录。
只是为了测试,我评论说,AND子句,然后它按预期工作,并返回55 + 4 =总共59个记录。
如何解决此问题与CTE。哪里不对?
CREATE OR REPLACE PROCEDURE CUSTCONNECT.sp_GetPodConfigurationGridData(
p_podURL IN varchar2, --PodUrl
p_serverType IN varchar2,
p_serverName IN varchar2,
p_publishedDate IN date,
P_RECORDSET OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN P_RECORDSET FOR
--This has total of 4 Records
WITH PodServerRecords(Key, value, Overwrite, ServerName, ServerType, PublishDate) AS (
select
PC.KeyId as Key, KeyIdValue as value, 'Pod' as Overwrite, '' as ServerName, '' as Servertype, PC.PublishDate
from (select
Keylog.*, row_number() over (partition by Keylog.KeyId order by Keylog.PublishDate desc) as RowNu
from PodConfigLog_Tab Keylog
where Keylog.URL = p_podURL
and Keylog.PublishDate >= p_publishedDate and Keylog.PublishDate <= sysdate
) PC
where PC.RowNu = 1 and PC.IsActive = 'T'
UNION
select
PCBS.KeyId as Key, KeyIdValue as value, 'Server' as Overwrite, PCBS.ServerName, Servertype, PCBS.PublishDate
from (select
Serlog.*, PS.ServerType, row_number() over (partition by Serlog.KeyId order by Serlog.PublishDate desc) as RowNu
from PodConfigByServerLog_Tab Serlog
join PodServer_tab PS on PS.ServerName = Serlog.ServerName
and Serlog.URL = PS.URL
where Serlog.URL = p_podURL
and Serlog.ServerName = p_serverName
and Serlog.PublishDate >= p_publishedDate and Serlog.PublishDate <= sysdate
) PCBS
where PCBS.RowNu = 1 and PCBS.IsActive = 'T'
)
--This has total of 55 Records
select
PCK.KeyId as Key ,DefaultKeyIdValue as value,'Default' as Overwrite, '' as ServerName, '' as Servertype, PCK.PublishDate
from
(select
Keylog.*, row_number() over (partition by Keylog.KeyId order by Keylog.PublishDate desc) as RowNu
from PodConfigKeyLog_Tab Keylog
where Keylog.PublishDate >= p_publishedDate and Keylog.PublishDate <= sysdate
) PCK
join POD_TAB PS on PS.URL = p_podURL
where PCK.RowNu = 1 and PCK.IsActive = 'T'
--This And caluse should remove those 4 Records and total Records should be 51.
and PCK.KeyId not in (
select KeyId
from PodServerRecords
)
UNION
--This is total of 4 Records
SELECT
Key, value, Overwrite, ServerName, ServerType, PublishDate
FROM PodServerRecords
Order By Key;
END;
/
在您提供的不同查询之间很难找到相似之处。他们为什么应该返回相同的数字? – Rene
我想你应该删除'UNION'部分的查询,因为它正在从'PodServerRecords'读取这4条记录 – Rahul