Where Clause Logic

  • Hello, 

    I'm newer to SQL and need some assistance in modifying a query. The query has a few prompts as this is an SSRS report. I'm trying to adjust the query so it will include those individuals that have a title of 'Developer' and an ID of '1234' (call this employee field). Now I only want instances where there is an exact match of that title and employee field. All other records with the title 'Developer' should be omitted. Let me know if I need to clarify further. Thanks for any assistance. 


    WHERE (customer = @customer_number) AND (period >= @starting_period) AND (period <= @ending_period) AND
          (title IN ('Manager', 'Leader', 'Worker'))

  • ID of '1234' (call this employee field).

    Is this sufficient to ID an individual employee?  Are you trying to preserve the recordset from the original query and simply add this one employee?

  • ...
    WHERE ID = 1234
    AND Title = 'Developer'
    ...

    John

  • ...
    WHERE ID = 1234
    AND Title = 'Developer'
    ...

    
    


    I think the issue is the need to combine it with the previous criteria.

    
    

  • Difficult to tell exactly what you need, but if you want the other conditions to apply to "Developer" too, then do this:


    WHERE (customer = @customer_number) AND (period >= @starting_period) AND (period <= @ending_period) AND
      ( (title IN ('Manager', 'Leader', 'Worker')) OR ((title IN 'Developer') AND (ID = '1234')) )

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • ScottPletcher - Thursday, April 19, 2018 8:22 AM

    Difficult to tell exactly what you need, but if you want the other conditions to apply to "Developer" too, then do this:


    WHERE (customer = @customer_number) AND (period >= @starting_period) AND (period <= @ending_period) AND
      ( (title IN ('Manager', 'Leader', 'Worker')) OR ((title IN 'Developer') AND (ID = '1234')) )

    Thanks this is on the right path. I'm still getting an error but will review my code again. Just to verify I don't need an extra parenthesis with the 'In Developer' piece?

  • JR11 - Thursday, April 19, 2018 9:05 AM

    ScottPletcher - Thursday, April 19, 2018 8:22 AM

    Difficult to tell exactly what you need, but if you want the other conditions to apply to "Developer" too, then do this:


    WHERE (customer = @customer_number) AND (period >= @starting_period) AND (period <= @ending_period) AND
      ( (title IN ('Manager', 'Leader', 'Worker')) OR ((title IN 'Developer') AND (ID = '1234')) )

    Thanks this is on the right path. I'm still getting an error but will review my code again. Just to verify I don't need an extra parenthesis with the 'In Developer' piece?

    Oops, yes, you do.  All the other extra parentheses just make it hard to read, at least for me.  I'd prefer simply this:


    WHERE customer = @customer_number AND
        period >= @starting_period AND
        period <= @ending_period AND 
        ( title IN ('Manager', 'Leader', 'Worker') OR
          (title IN ('Developer') AND ID = '1234') )

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Fantastic! That worked! Thanks for taking the time for all to respond to my request. 

    Joe

Viewing 8 posts - 1 through 7 (of 7 total)

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