一旦条件满足,是否可以让SQL停止检查WHERE子句?例如,如果我有如下声明:
SELECT * FROM Table1
WHERE Table1.SubID = (SELECT TOP 1 SubID FROM Table2 ORDER BY Date DESC)
OR Table1.OrderID = (SELECT TOP 1 OrderID FROM Table2 ORDER BY Date DESC)
第一次检查后是否可以停止执行?实质上,只应使用where子句中的两个检查中的一个,优先于第一个。以下示例案例。
示例案例:
案例1
Table1 SubID=600 OrderID=5
Table2 TOP 1 SubID=NULL
Table2 TOP 1 OrderID=5
Matches the OrderID to 5
案例2
Table1 SubId=600 OrderId=5
Table2 Top 1 SubID=600
Table2 Top 1 OrderID=3
Matches to SubID=600, not OrderID=3
鉴于建议的答案,with
似乎是解决SQL本身无法做到的最佳解决方案。对于我的具体情况,尝试将其置于outer apply
时会出现问题,如下所示。
SELECT * FROM tbl_MainFields
OUTER APPLY
(
WITH conditional AS
(
SELECT 1 AS 'choice', PlanCode, Carrier
FROM tbl_payers
WHERE tbl_payers.PlanCode =
(
SELECT TOP 1 PlanCode
FROM tbl_payerDenials
WHERE tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
UNION ALL
SELECT 2 AS 'choice', PlanCode, Carrier
FROM tbl_payers
WHERE tbl_payers.OrderNum =
(
SELECT TOP 1 DenialLevel
FROM tbl_payerDenials
WHERE tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
)
SELECT
PlanCode AS DenialPC,
Carrier AS DenialCAR
FROM conditional
WHERE choice = (SELECT MIN(choice) FROM conditional)
) denialData
答案 0 :(得分:4)
我认为你可以尝试这样的事情
WITH conditional AS(
SELECT 1 AS 'choice', PlanCode, Carrier
FROM tbl_payers
WHERE tbl_payers.PlanCode =
(
SELECT TOP 1 PlanCode
FROM tbl_payerDenials
JOIN tbl_mainFields ON
tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
UNION ALL
SELECT 2 AS 'choice', PlanCode, Carrier
FROM tbl_payers
WHERE tbl_payers.OrderNum =
(
SELECT TOP 1 DenialLevel
FROM tbl_payerDenials
JOIN tbl_mainFields ON
tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
)
SELECT * FROM tbl_MainFields tMF
OUTER APPLY
(
SELECT *
FROM conditional c
WHERE c.choice = (SELECT MIN(choice) FROM conditional)
) denialData
我正在使用1
和2
值来mark
查询,然后从第一个中选择信息,如果它返回值,否则返回第二个值查询(MIN(choice)
部分)。
我希望很清楚。
答案 1 :(得分:0)
不,这不可能。查询优化器在发送查询时会优化整个查询。此外,它还会创建一个计划来利用整个查询。如果你需要这样做,你会想看看这样的事情:
SELECT *
INTO #tbl
FROM Table1
WHERE Table1.SubID = (SELECT TOP 1 SubID FROM Table2 ORDER BY Date DESC)
IF ( NOT EXISTS (SELECT * FROM #tbl) )
BEGIN
SELECT *
INTO #tbl
FROM Table1
WHERE Table1.OrderID = (SELECT TOP 1 OrderID FROM Table2 ORDER BY Date DESC)
END
答案 2 :(得分:-1)
归功于@RaduGheorghiu的灵感。
此功能类似于建议的WITH
和MIN
组合,但允许在OUTER APPLY
SELECT * FROM tbl_MainFields
OUTER APPLY
(
SELECT TOP 1
PlanCode AS DenialPC,
Carrier AS DenialCAR,
Precedence
FROM
(
SELECT
1 AS 'Precedence',
PlanCode,
Carrier
FROM tbl_payers
WHERE tbl_payers.PlanCode =
(
SELECT TOP 1 PlanCode
FROM tbl_payerDenials
WHERE tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
UNION ALL
SELECT
2 AS 'Precedence',
PlanCode,
Carrier
FROM tbl_payers
WHERE tbl_payers.OrderNum =
(
SELECT TOP 1 DenialLevel
FROM tbl_payerDenials
WHERE tbl_payerDenials.AccountNumber = tbl_mainFields.AccountNumber
ORDER BY InsertDate DESC
)
) AS denialPrecedence
ORDER BY Precedence
) denialData