简体   繁体   中英

Conditional Join statement in SQL Server

I want to implement conditional join statement in a way if first condition pass then select record based on first condition else go to second join condition..

Select 
    col1, Col2, Col3, TableB.Column1, TableB.Column2 
from 
    TableA
JOIN 
    TableB ON (TableA.col1 = TableB.Column1
               TableA.col2 = TableB.Column2
               TableA.col3 = TableB.Column3)
              OR //second OR
              (
               TableA.col1 = TableB.Column1
               TableA.col2 = TableB.Column2
               TableB.Column3 IS NULL
              )
             OR //Third OR
              (
               TableA.col1 = TableB.Column1
               TableA.col3 = TableB.Column3
               TableB.Column2 IS NULL
              )

If condition one passed, query should not go to second OR condition and I should get one row as output only.

If I have understood your requirements correctly, then the following query is what you are looking for:

SELECT col1, Col2, Col3
FROM (
  SELECT col1, Col2, Col3,
         RANK() OVER (ORDER BY seqNo) AS rn 
  FROM (
    -- Query no. 1
    SELECT col1, Col2, Col3, 1 AS seqNo 
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableA.col2 = TableB.Column2
                        AND TableA.col3 = TableB.Column3

    UNION ALL

    -- Query no. 2
    SELECT col1, Col2, Col3, 2 AS seqNo
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableA.col2 = TableB.Column2
                        AND TableB.Column3 IS NULL

    UNION ALL

    -- Query no. 3
    SELECT col1, Col2, Col3, 3 AS seqNo 
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableB.Column2 IS NULL
                        AND TableA.col3 = TableB.Column3 ) AS t 
) AS s
WHERE s.rn = 1

The above will return the first non-empty set of the three separate queries being UNION ed.

This means if query no. 1 returns a non-empty set, then the rows of this set and only these are going to be returned. Otherwise rows of query no. 2 are returned, etc.

If I have understood the requirements correctly, the following should return the desired results:

SELECT col1, Col2, Col3, column1, column2
FROM (
  SELECT col1, Col2, Col3, column1, column2,
         RANK() OVER (PARTITION BY col1, Col2, Col3 ORDER BY seqNo) AS rn 
  FROM (
    -- Query no. 1
    SELECT col1, Col2, Col3, column1, column2, 1 AS seqNo 
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableA.col2 = TableB.Column2
                        AND TableA.col3 = TableB.Column3
    UNION ALL
    -- Query no. 2
    SELECT col1, Col2, Col3, column1, column2, 2 AS seqNo
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableA.col2 = TableB.Column2
                        AND TableB.Column3 IS NULL
    UNION ALL
    -- Query no. 3
    SELECT col1, Col2, Col3, column1, column2, 3 AS seqNo 
    FROM TableA
    INNER JOIN TableB ON TableA.col1 = TableB.Column1 
                        AND TableB.Column2 IS NULL
                        AND TableA.col3 = TableB.Column3 ) AS t 
) AS s
WHERE s.rn = 1

SQLFiddle here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM