我的老板把这段代码扔给我了,我很难理解内部连接的最后一个 ON 语句是如何工作的。我认为他也不完全理解它(但它完成了工作)。真的只是对了解有关 SQL 工作原理的更多信息感兴趣。非常感谢!
这是 On 声明
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 相同,因此没有最终数据?感谢您的帮助!