2017-04-24 153 views
0

我對sql很陌生,我試圖獲取所有其他表中沒有記錄的項目。例如;有許多項目和項目有很多來源,但有些項目根本沒有任何來源。我只想檢索在其他表中沒有任何記錄的項目......這是我嘗試的代碼,但它仍然給我一些有源代碼的項目。任何人都可以幫助我嗎?謝謝!獲取其他表中沒有記錄的所有對象

SELECT   
        p_Project.Project_ID as Id 
        , p_Project.ProjectNum 
        , p_Project.ProjectName 
        , p_Project.Client_ID 
        , p_Project.Industry 
        , p_Project.ProjectStatus 
        , p_Project.EffectiveDate 
        , p_Project.ProjectOrigination 
        , p_Project.ProjDateClosed 
        , p_Project.PrimaryMaterial 
        , p_Project.PrimaryService 
        , p_Project.PrimarySource 
        , p_Project.ProjectCategory 
        , p_Client.Client_ID as Id 
        , p_Client.ClientName 
      FROM p_Project 
       LEFT OUTER JOIN 
        p_Client ON p_Project.Client_ID = p_Client.Client_ID 
      WHERE p_Project.Project_ID IN ( SELECT p_Project.Project_ID 
           FROM p_Project 
           LEFT OUTER JOIN p_Client ON p_Project.Client_ID = p_Client.Client_ID WHERE p_Project.Project_ID IN (SELECT p_Project.Project_ID as Id FROM p_Project WHERE p_Project.ProjectStatus ='active') AND p_Project.Project_ID IN (SELECT p_Project.Project_ID as Id FROM p_Project LEFT OUTER JOIN p_Project_Pricing ON p_Project.Project_ID = p_Project_Pricing.Project_ID WHERE ( p_Project_Pricing.SourceCode IS NULL OR p_Project_Pricing.SourceCode LIKE N'') AND (p_Project.PrimarySource IS NULL OR p_Project.PrimarySource LIKE N'')) 
           GROUP BY p_Project.Project_ID, p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum 
           ORDER BY p_Client.ClientName, p_Project.ProjectName, p_Project.ProjectNum 
           OFFSET (0) ROWS FETCH NEXT (25) ROWS ONLY) 
+0

請考慮在格式化文本中添加樣本表數據和預期結果。 – TheGameiswar

回答

1

請嘗試以下操作。

select proj.* from [yourprojtable] proj 
left join [yoursourcetable] src 
on proj.projid = src.projid 
where src.projid is null 

在您的查詢中,嘗試將where條件更改爲'where P_Client.Client_id is null'。

+0

謝謝!這真的對我有用! – BYG

+0

@BYG如果你覺得這個有用,你也可以接受這個答案。 – Coder1991

相關問題