Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Or with a stop after first check

Is it possible to have SQL stop checking the WHERE clause once a condition is met? For instance, if I have a statement as below:

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)

Is it possible to stop execution after the first check? In essence, only one of the two checks in the where clause should be used, giving precedence to the first. Example cases below.

Example Cases:

Case1

Table1 SubID=600 OrderID=5

Table2 TOP 1 SubID=NULL

Table2 TOP 1 OrderID=5

Matches the OrderID to 5

Case 2

Table1 SubId=600 OrderId=5

Table2 Top 1 SubID=600

Table2 Top 1 OrderID=3

Matches to SubID=600, not OrderID=3

Given suggested answers, a with seems the best possible solution to solve what SQL is not inherently able to do. For my specific situation, the issue comes when attempting to put this into an outer apply, as below.

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
like image 844
steventnorris Avatar asked Sep 30 '13 12:09

steventnorris


People also ask

Does order of operations matter in SQL?

If the database follows a different order of operations, the performance of the query can decrease dramatically. The best way to learn SQL order of operations is through practice.

How do I do an IF THEN statement in SQL?

Syntax. IF (a <= 20) THEN c:= c+1; END IF; If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing end if) will be executed.

Does the order of filters matter in SQL?

The order of filters in the WHERE clause does not matter. The SFDC Optimizer evaluates all filters to look for the indexed and most selective one.

Should you use SELECT * in SQL?

Conclusion. That's all about why you should not use SELECT * in SQL query anymore. It's always better to use the explicit column list in the SELECT query than a * (star) wildcard. It not only improves the performance but also makes your code more explicit.


1 Answers

I think you can try something like this

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

I'm using the 1 and 2 values to mark the queries, and then select the information from the first, if it returns values, otherwise it returns values from the second query (the MIN(choice) part).

I hope it is clear.

like image 55
Radu Gheorghiu Avatar answered Oct 20 '22 01:10

Radu Gheorghiu