SQL 服务器 - 操作顺序难度
SQL Server - Order of Operations difficulty
试图弄清楚如何正确地排序以获得预期的结果。我正在寻找的是 A 为真,并且 B、C 或 D 中的任何一个也为真。 Starting_Date 和 Ending_Date 在代码的前面定义。
- 如果我使用如下所示的 A && ((B)||(C)||(D)),则不会返回任何结果。
- 如果我用A && (B || C || D),我认为在逻辑上与上述相同,同样returns没有结果。
- 如果我用A && B || C || D,我认为A组和B组在一起,我得到几千个结果
- 如果我在 Between 语句两边加上括号,我会得到一个错误(语法无效)
Declare @Starting_Date date = '2019-01-01'
Declare @Ending_Date date = '2019-01-31'
Select x
From y
Where REPORTING_UNIT = '36B11' --A
AND ((Closing_Date BETWEEN @Starting_Date AND @Ending_Date) --B Closing date is between report date range (eg: episode already open before period)
OR (Opening_Date BETWEEN @Starting_Date AND @Ending_Date) --C Opening date is between report date range (eg: episode was opened at some point during the period)
OR (Reverse_Opening_Date BETWEEN @Starting_Date AND @Ending_Date)) --D Reverse opening date is between report date range (eg: episode was closed at some point, then reopened during the period)
预期结果:
1858 年是数据库表示“未关闭”的方式。因此,在此示例中,所有这些都将包括在内,但第 14 行除外,该行在我们定义的结束日期之前关闭。第 5 和第 6 行也将包括在内,因为该剧集在我们评估期间开放。
编辑:添加了编码的开始和结束日期以及一些预期结果。
以下是您的代码,减去了 OR 序列中的冗余括号,应该可以工作。
你对@Starting_Date和@Ending_Date的值有信心吗?
Select x
From y
Where REPORTING_UNIT = '36B11' --A
AND (Closing_Date BETWEEN @Starting_Date AND @Ending_Date --B Closing date is between report date range (eg: episode already open before period)
OR Opening_Date BETWEEN @Starting_Date AND @Ending_Date --C Opening date is between report date range (eg: episode was opened at some point during the period)
OR Reverse_Opening_Date BETWEEN @Starting_Date AND @Ending_Date) --D Reverse opening date is between report date range (eg: episode was closed at some point, then reopened during the period)
保持
REPORTING_UNIT = '36B11' AND Closing_Date BETWEEN @Starting_Date AND @Ending_Date
条件在 WHERE
子句中,查看您获得的结果,然后将 OR
条件一一添加,您可能会明白为什么会发生这种情况。此外,在粗略地浏览了上面的数据之后,逻辑上应该只包含第 5 行。 BETWEEN
子句将只检查日期范围和第 5 行是否满足。
试图弄清楚如何正确地排序以获得预期的结果。我正在寻找的是 A 为真,并且 B、C 或 D 中的任何一个也为真。 Starting_Date 和 Ending_Date 在代码的前面定义。
- 如果我使用如下所示的 A && ((B)||(C)||(D)),则不会返回任何结果。
- 如果我用A && (B || C || D),我认为在逻辑上与上述相同,同样returns没有结果。
- 如果我用A && B || C || D,我认为A组和B组在一起,我得到几千个结果
- 如果我在 Between 语句两边加上括号,我会得到一个错误(语法无效)
Declare @Starting_Date date = '2019-01-01'
Declare @Ending_Date date = '2019-01-31'
Select x
From y
Where REPORTING_UNIT = '36B11' --A
AND ((Closing_Date BETWEEN @Starting_Date AND @Ending_Date) --B Closing date is between report date range (eg: episode already open before period)
OR (Opening_Date BETWEEN @Starting_Date AND @Ending_Date) --C Opening date is between report date range (eg: episode was opened at some point during the period)
OR (Reverse_Opening_Date BETWEEN @Starting_Date AND @Ending_Date)) --D Reverse opening date is between report date range (eg: episode was closed at some point, then reopened during the period)
预期结果:
1858 年是数据库表示“未关闭”的方式。因此,在此示例中,所有这些都将包括在内,但第 14 行除外,该行在我们定义的结束日期之前关闭。第 5 和第 6 行也将包括在内,因为该剧集在我们评估期间开放。
编辑:添加了编码的开始和结束日期以及一些预期结果。
以下是您的代码,减去了 OR 序列中的冗余括号,应该可以工作。
你对@Starting_Date和@Ending_Date的值有信心吗?
Select x
From y
Where REPORTING_UNIT = '36B11' --A
AND (Closing_Date BETWEEN @Starting_Date AND @Ending_Date --B Closing date is between report date range (eg: episode already open before period)
OR Opening_Date BETWEEN @Starting_Date AND @Ending_Date --C Opening date is between report date range (eg: episode was opened at some point during the period)
OR Reverse_Opening_Date BETWEEN @Starting_Date AND @Ending_Date) --D Reverse opening date is between report date range (eg: episode was closed at some point, then reopened during the period)
保持
REPORTING_UNIT = '36B11' AND Closing_Date BETWEEN @Starting_Date AND @Ending_Date
条件在 WHERE
子句中,查看您获得的结果,然后将 OR
条件一一添加,您可能会明白为什么会发生这种情况。此外,在粗略地浏览了上面的数据之后,逻辑上应该只包含第 5 行。 BETWEEN
子句将只检查日期范围和第 5 行是否满足。