Conditional Join

  • I have 2 tables

    1-Web_Orders

    Reference_No int

    Released int

    Data :

    Reference_No , Released

    123 ,0

    456,1

    789,1

    002,1

    2-tblOptionalRefNo

    Reference_No int

    Data:

    Reference_No

    123

    Table 2 is an optional . Sometime it can have data and sometimes not. so If it has data, we need to fetch data on the basis of table 2 else fetch data from table1. Table2 will have Reference_No those exist in table1 , so the purpose is to filter data only.

    e.g. in above sample data, it should return 123

    If tblOptionalRefNo is empty , it should return

    123

    456

    789

    002

    I wrote following query , it is returning results if table2 has data, but when table2 is empty ,it don't display results. what could be problem ?

    SELECT wo.Reference_No

    FROM Web_Orders wo

    INNER JOIN dbo.tblOptionalRefNo OO

    ON WO.Reference_No = CASE WHEN OO.Reference_No is null THEN WO.Reference_No

    ELSE OO.Reference_No

    END

    WHERE wo.Released IN (0,1)

    GROUP BY wo.Reference_No

    HAVING COUNT(1) = 1

  • Why don't you use left outer join?

    😎

  • because if table2 has Reference_No, i want to display only these. Left join will display table1 data all the time

  • thbaig (1/3/2017)


    because if table2 has Reference_No, i want to display only these. Left join will display table1 data all the time

    Can you write a better description of what you are trying to do? So far your description is erratic and ambiguous.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • So do the left join and then use COALESCE to pick up t2 data if it exists:

    select ColName = coalesce(t2.col,t1.col)

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • thbaig (1/3/2017)


    because if table2 has Reference_No, i want to display only these. Left join will display table1 data all the time

    Do you mean this?

    SELECT Reference_No

    FROM (

    SELECT Reference_No

    FROM dbo.tblOptionalRefNo

    UNION ALL

    SELECT wo.Reference_No

    FROM Web_Orders wo

    WHERE wo.Released IN (0,1)

    AND NOT EXISTS(SELECT 1 FROM dbo.tblOptionalRefNo OO WHERE OO.Reference_No = WO.Reference_No)

    ) d

    GROUP BY Reference_No

    HAVING COUNT(*) = 1

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • No. It return all rows all time.

    I have updated description

  • thbaig (1/3/2017)


    No. It return all rows all time.

    I have updated description

    If you want results from either one table or the other, then use IF EXISTS()

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Phil Parkin (1/3/2017)


    So do the left join and then use COALESCE to pick up t2 data if it exists:

    select ColName = coalesce(t2.col,t1.col)

    If following is as per suggestion, it return all data and not filter if table2 has Reference_No

    SELECT coalesce(oo.Reference_No,wo.Reference_No) as Reference_No

    FROM Web_Orders wo

    left JOIN dbo.tblOptionalRefNo OO

    ON WO.Reference_No = OO.Reference_No

    Released IN (0,1)

    GROUP BY coalesce(oo.Reference_No,wo.Reference_No)

    HAVING COUNT(1) = 1

  • thbaig (1/3/2017)


    I have 2 tables

    1-Web_Orders

    Reference_No int

    Released int

    Data :

    Reference_No , Released

    123 ,0

    456,1

    789,1

    002,1

    2-tblOptionalRefNo

    Reference_No int

    Data:

    Reference_No

    123

    Table 2 is an optional . Sometime it can have data and sometimes not. so If it has data, we need to fetch data on the basis of table 2 else fetch data from table1. Table2 will have Reference_No those exist in table1 , so the purpose is to filter data only.

    e.g. in above sample data, it should return 123

    If tblOptionalRefNo is empty , it should return

    123

    456

    789

    002

    I wrote following query , it is returning results if table2 has data, but when table2 is empty ,it don't display results. what could be problem ?

    SELECT wo.Reference_No

    FROM Web_Orders wo

    INNER JOIN dbo.tblOptionalRefNo OO

    ON WO.Reference_No = CASE WHEN OO.Reference_No is null THEN WO.Reference_No

    ELSE OO.Reference_No

    END

    WHERE wo.Released IN (0,1)

    GROUP BY wo.Reference_No

    HAVING COUNT(1) = 1

    I think you need a slightly different approach:

    SELECT WO.*

    FROM Web_Orders AS WO

    INNER JOIN tblOptionalRefNo AS ORN

    ON WO.Reference_No = ORN.Reference_No

    UNION ALL

    SELECT W.*

    FROM Web_Orders AS W

    WHERE NOT EXISTS (

    SELECT 1

    FROM Web_Orders AS W2

    INNER JOIN tblOptionalRefNo AS O2

    ON W2.Reference_No = O2.Reference_No

    );

    Let me know if that works...

  • thbaig (1/3/2017)


    Phil Parkin (1/3/2017)


    So do the left join and then use COALESCE to pick up t2 data if it exists:

    select ColName = coalesce(t2.col,t1.col)

    If following is as per suggestion, it return all data and not filter if table2 has Reference_No

    SELECT coalesce(oo.Reference_No,wo.Reference_No) as Reference_No

    FROM Web_Orders wo

    left JOIN dbo.tblOptionalRefNo OO

    ON WO.Reference_No = OO.Reference_No

    Released IN (0,1)

    GROUP BY coalesce(oo.Reference_No,wo.Reference_No)

    HAVING COUNT(1) = 1

    I was assuming that your 'tblOptionalRefNo' table contained other data, not just reference numbers.

    So let's say we have Web_Orders(Reference_No, Col1) and tblOptionalRefNo(Reference_No, Col1).

    My assumption was that you wanted to display Col1 from tblOptionalRefNo, if a match on Reference_No was found. Otherwise display Col1 from Web_Orders.

    select Col1 = coalesce(orn.Col1, wo.Col1)

    from Web_Orders wo

    left join tblOptionalRefNo orn on wo.Reference_No = orn.Reference_No

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply