【发布时间】:2013-03-19 06:34:41
【问题描述】:
我的老板把这段代码扔给我了,我很难理解内部连接的最后一个 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 相同,因此没有最终数据?感谢您的帮助!
【问题讨论】:
-
我认为我们需要更多地了解您的报告表才能完全回答这个问题。您能否更新您的问题,以便 X 和 A 中的示例数据也具有 Report.ID。我知道正在发生的事情,但我不能说没有看到 ID,因为它们是连接条件的一部分。
-
结果数据来自一个id。所以认为这个结果集在底部有一个 where 语句 where id = '111';感谢您的帮助!
-
恕我直言,如果这个条件在 WHERE 子句中会更清楚。但是,我不确定执行计划是否相同。
标签: mysql sql sql-server-2008 subquery inner-join