我的老闆以我的方式拋出了這段代碼,我很難理解內部連接的最後ON語句是如何工作的。我不認爲他完全理解它(但它完成了這項工作)。真的只是想了解更多關於SQL如何工作的知識。非常感謝!>,<, <>,=內部連接條件的運算符
這裏是在聲明
and (A.Submitted_Date > X.Submitted_Date)))
這裏是查詢
SELECT AA.ID, AA.Submitted_Date as Date_Status
FROM Report as AA
where AA.Submitted_Date in
--START
(
SELECT X.Submitted_Date
FROM Report as A
inner join
--Start Find All Dates Submitted
(
SELECT [ID],[Submitted_Date]
FROM Report
where not(Submitted_Date is null and Cleared_Date is null)
group by ID, Submitted_Date) as X
--End Find all Dates Submittd
--below is the conditions of the join
ON A.ID = X.ID
and A.ID= AA.ID
--THIS IS THE CONDITION I AM CONFUSED ABOUT!!!!
and (A.Submitted_Date > X.Submitted_Date)))
group by X.Submitted_Date)
and not AA.Submitted_Date is null
group by AA.ID, AA.Submitted_Date
下面是日期的樣品從表A
2012-11-27 00:00:00.000
2012-11-27 00:00:00.000
2012-11-27 00:00:00.000
2012-12-10 00:00:00.000
2012-11-27 00:00:00.000
2012-11-27 00:00:00.000
2012-11-29 00:00:00.000
2012-12-05 00:00:00.000
2012-12-12 00:00:00.000
下面是日期的樣本來自表X
2012-11-27 00:00:00.000
2012-11-29 00:00:00.000
2012-12-05 00:00:00.000
2012-12-10 00:00:00.000
2012-12-12 00:00:00.000
這裏是之前的最後一個條件
2012-11-27 00:00:00.000
2012-11-29 00:00:00.000
2012-12-05 00:00:00.000
2012-12-10 00:00:00.000
2012-12-12 00:00:00.000
的結果這裏是A.Sub結果> X.Sub
2012-11-27 00:00:00.000
2012-11-29 00:00:00.000
2012-12-05 00:00:00.000
2012-12-10 00:00:00.000
我爲什麼這些日期出現混亂。 A和X之間正在進行什麼比較? A中的值不會總是與X相同,因此導致沒有最終的數據?感謝您的幫助!
恕我直言,它會更清楚,如果這種情況是在WHERE子句。但是,我不確定執行計劃是否相同。 –
我認爲我們需要更多地瞭解您的報告表以完全回答這個問題。你能否更新你的問題,以便X和A的樣本數據也有Report.ID。我對發生了什麼有了一個概念,但是我不能說沒有看到ID,因爲它們是連接條件的一部分。 – Fodagus
生成的數據來自一個ID。所以把這個結果集想象成在底部有一個where語句,其中id ='111';感謝您試圖幫助! –