selecting from 2 versions of join based on a variable value

  • I need to do a select with a join from a table in the form of a simple select.

    SELECT

    bla bla

    FROM Table1

    left join

    (SELECT bla bla FROM TABLE2)

    on something

    Based on the value of a variable i want to either join on one select or on a different select. Is there anything like CASE or IF, that can be used to decide, which part of code to use in the join?

    I'd need something like this:

    SELECT

    blah blah

    FROM Table1

    CASE @Variable = 1 THEN

    left join

    (SELECT blah blah FROM TABLE2)

    ELSE

    left join

    (SELECT something else FROM TABLE2)

    on something

  • Something very similar was discussed several times during the last few days.

    This is one of the threads:

    Dynamically choosing inner or outer join

    I think that similar approach could work for you as well. Also you can visit the link I posted there in my response, which leads to article dealing with various possibilities.

    It won't be precisely what you need, but IMHO it should get you started pretty well.

    EDIT : A lot depends on what is is this "blabla" in the SELECT. If you have troubles follwing the advice, post more info about your query.

Viewing 2 posts - 1 through 1 (of 1 total)

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