2010-11-30 74 views
5

我有兩個表。一個是提交給我們的報告表格。另一個是臨時表格,其中包含最終應提交給我們的報告記錄。我想只顯示臨時表中與記錄表中的記錄不匹配的記錄(因此仍顯示必須提交的報告)。查詢顯示錶之間不匹配的記錄

實例數據是:

Reports table: 

CREATE TABLE [dbo].[Reports] 
( 
    [ReportID] [int] IDENTITY(1,1) NOT NULL, 
    [ReportDate] [date] NULL, 
    [AssessmentID] [int] NOT NULL, 
    [ReportType] [varchar](50) NULL 
); 

AssessmentID ReportType ReportID 
1 1st Quarterly 27 
2 1st Quarterly 30 
2 2nd Quarterly 31 
2 3rd Quarterly 32 

QuarterlyReportsDue table: 

CREATE TABLE #QuarterlyReportsDue 
( 
AssessmentID INT, 
InstallationDate DATE, 
QuarterlyReportType VARCHAR(50) 
); 

AssessmentID InstallationDate QuarterlyReportType 
1 2009-08-14 1st Quarterly 
1 2009-08-14 2nd Quarterly 
1 2009-08-14 3rd Quarterly 
1 2009-08-14 4th Quarterly 
2 2008-05-16 4th Quarterly 
2 2008-05-16 3rd Quarterly 
2 2008-05-16 2nd Quarterly 
2 2008-05-16 1st Quarterly 

我已經試過左外連接,但我遇到了問題。請參閱我下面的SQL:

SELECT #QuarterlyReportsDue.InstallationDate, #QuarterlyReportsDue.QuarterlyReportType, Reports.ReportType 
FROM #QuarterlyReportsDue 
LEFT OUTER JOIN Reports ON #QuarterlyReportsDue.AssessmentID = Reports.AssessmentID 
WHERE Reports.ReportType IN ('1st Quarterly', '2nd Quarterly', '3rd Quarterly', '4th Quarterly') 
AND Reports.ReportType <> #QuarterlyReportsDue.QuarterlyReportType 
ORDER BY #QuarterlyReportsDue.AssessmentID 

而且我的結果:

AssessmentID QuarterlyReportType ReportType ReportID 
1 2nd Quarterly 1st Quarterly 27 
1 3rd Quarterly 1st Quarterly 27 
1 4th Quarterly 1st Quarterly 27 
2 4th Quarterly 1st Quarterly 30 
2 4th Quarterly 2nd Quarterly 31 
2 4th Quarterly 3rd Quarterly 32 
2 1st Quarterly 2nd Quarterly 31 
2 1st Quarterly 3rd Quarterly 32 
2 3rd Quarterly 1st Quarterly 30 
2 3rd Quarterly 2nd Quarterly 31 
2 2nd Quarterly 1st Quarterly 30 
2 2nd Quarterly 3rd Quarterly 32 

對於評估1它的偉大工程,評估2有很多重複。我怎樣才能解決這個問題,只顯示理想的結果?

AssessmentID QuarterlyReportType ReportType 
1 2nd Quarterly 1st Quarterly 
1 3rd Quarterly 1st Quarterly 
1 4th Quarterly 1st Quarterly 
2  4th Quarterly  

回答

5

當你LEFT JOIN到一個表,然後引用該表的列的一個WHERE子句中,您隱轉聯接成一個INNER JOIN。相反,將這些條件移出WHERE並使其成爲JOIN條件的一部分。

SELECT q.InstallationDate, q.QuarterlyReportType, Reports.ReportType 
    FROM #QuarterlyReportsDue q 
     LEFT OUTER JOIN Reports r 
      ON q.AssessmentID = r.AssessmentID 
       AND q.QuarterlyReportType = r.ReportType 
       AND r.ReportType IN ('1st Quarterly', '2nd Quarterly', '3rd Quarterly', '4th Quarterly') 
    WHERE r.AssessmentID IS NULL /* matching record not found in Reports table */ 
    ORDER BY #QuarterlyReportsDue.AssessmentID 
+0

也很棒!感謝您的解釋。 – 2010-11-30 20:47:49

3

您應該使用NOT EXISTS來查找臨時表中沒有提交的報表中的條目的條目。

+0

+1提NOT EXISTS。 – Aliostad 2010-11-30 20:17:15

2

這個連接會將兩個表中的記錄相互疊加(笛卡爾),這就是爲什麼你會獲得更多記錄回來。

請記住,您正在加入AssessmentId並且有多個記錄與assessmentId相同。雖然您正在過濾掉那些不具有相同ReportType但您碰到新組合

SELECT #QuarterlyReportsDue.InstallationDate, 
     #QuarterlyReportsDue.QuarterlyReportType 
FROM #QuarterlyReportsDue 
WHERE NOT EXISTS 
     (
     SELECT 1 
     FROM Reports 
     WHERE Reports.ReportType = QuarterlyReportsDue.QuarterlyReportType 
     AND #QuarterlyReportsDue.AssessmentID = Reports.AssessmentID 
    ) 
2

也許這樣的事情?

SELECT  qrd.InstallationDate, 
      qrd.QuarterlyReportType 
FROM  #QuarterlyReportsDue qrd 
WHERE  qrd.QuarterlyReportType IN ('1st Quarterly', '2nd Quarterly', '3rd Quarterly', '4th Quarterly') 
      AND NOT EXISTS (
       SELECT 1 
       FROM  Reports r 
       WHERE r.AssessmentID = qrd.AssessmentID 
          AND r.ReportType = qrd.QuarterlyReportType 
      ) 
ORDER BY qrd.AssessmentID 
+0

完美的作品。謝謝! – 2010-11-30 20:45:29

0
select * 
from Reports r 
left join #QuarterlyReportsDue d 
    on d.AssessmentID=r.AssessmentID 
    and d.QuarterlyReportType=ReportType 
where d.AssessmentID is null 
相關問題