2015-09-03 80 views
-1

我有以下查詢。 以下查詢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; 
/
+0

在您提供的不同查詢之間很難找到相似之處。他們爲什麼應該返回相同的數字? – Rene

+1

我想你應該刪除'UNION'部分的查詢,因爲它正在從'PodServerRecords'讀取這4條記錄 – Rahul

回答

3

您的問題是,CTE沒有名爲KeyId的列。您將其重命名爲Key。但是,NOT INNULL值時出現意外的行爲。您可以通過直接消除它們解決這個問題:

PCK.KeyId not in (
     select psr.KeyId 
     from PodServerRecords psr 
     where psr.KeyId IS NOT NULL 
    ) 

我建議使用NOT EXISTS代替:

NOT EXISTS (select 1 
      from PodServerRecords psr 
      where psr.KeyId = PCK.KeyId 
      ) 

這可能會解決您的問題。

其實,我在CTE中看不到KeyId。所以,我認爲你想要:

NOT EXISTS (select 1 
      from PodServerRecords psr 
      where psr.Key = PCK.KeyId 
      ) 

注意前面的語句將返回錯誤,表明問題是錯誤的列。

0

我認爲你應該刪除你的查詢的UNION部分因爲它正在從PodServerRecords讀取這4條記錄。您的查詢應該是

--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 
    );