need help with Sql statement using case and join together

  • Hello I am trying to use this logic into a query:

    Select P.S,E.S,E.R

    from Pack P(nolock)

    join Exp E on P.Id=E.O

    on E.R is null

    case when E.R is not null then ''

    else ''

    end

    where P.s='PLT000044'

    I have to query two conditions joining the tables. when E.R is NULL and when E.R is not null. but the value is coming from the join between the 2 tables 😛 and E

    please help me writing this query using either case or if

    Thank you

  • montserrat.deza (1/29/2015)


    Hello I am trying to use this logic into a query:

    Select P.S,E.S,E.R

    from Pack P(nolock)

    join Exp E on P.Id=E.O

    on E.R is null

    case when E.R is not null then ''

    else ''

    end

    where P.s='PLT000044'

    I have to query two conditions joining the tables. when E.R is NULL and when E.R is not null. but the value is coming from the join between the 2 tables 😛 and E

    please help me writing this query using either case or if

    Thank you

    You have simplified your query so much it doesn't make much sense. What is the join condition and what are you trying to do?

    I think you want your join predicate to be something like this (I hope your real table and columns have better names):

    join Exp E on P.Id = case when E.R is not null then E.O else P.Id end

    Also, do you fully understand that NOLOCK hint? Are you ok with missing and/or duplicate rows being returned? If you are going to use that hint you need to provide the WITH keyword. Leaving that keyword off of table hints is deprecated and it will be required in the future. Here is one article discussing that hint. http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Lookup OR in Books Online, the help system for SQL Server. Also, look at the difference between IF, which conditionally runs statements, and CASE, which operates within a statement.


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]

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

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