1

I have a large stored procedure that is used to return results for a dialog with many selections. I have a new criteria to get "extra" rows if a particular bit column is set to true. The current setup looks like this:

SELECT
  CustomerID,
  FirstName,
  LastName,
  ...
FROM HumongousQuery hq
LEFT JOIN (
    -- New Query Text
) newSubQuery nsq ON hq.CustomerID = nsq.CustomerID

I have the first half of the new query:

SELECT DISTINCT 
    c.CustomerID,
    pp.ProjectID,
    ep.ProductID
FROM Customers c
JOIN Evaluations e (NOLOCK)
    ON c.CustomerID = e.CustomerID
JOIN EvaluationProducts ep (NOLOCK)
    ON e.EvaluationID = ep.EvaluationID
JOIN ProjectProducts pp (NOLOCK)
    ON ep.ProductID = pp.ProductID
JOIN Projects p
    ON pp.ProjectID = p.ProjectID
WHERE 
    c.EmployeeID = @EmployeeID
    AND e.CurrentStepID = 5
    AND p.IsComplete = 0

The Projects table has a bit column, AllowIndirectCustomers, which tells me that this project can use additional customers when the value is true. As far as I can tell, the majority of the different SQL constructs are geared towards adding additional columns to the result set. I tried different permutations of the UNION command, with no luck. Normally, I would turn to a table-valued function, but I haven't been able to make it work with this scenerio.

This one has been a stumper for me. Any ideas?

4
  • How do you determine if the customer is to be included? Both when you allow indirect and when you don't. Commented Sep 20, 2013 at 20:55
  • The query above is for direct assignments to a specific employee. This one is used regardless of the bit flag. Basically, indirects are customers that are assigned to the employee, have the same product, but are in a different project. Commented Sep 20, 2013 at 20:59
  • Check below and let me know if that works for you. Commented Sep 20, 2013 at 21:06
  • Alright, going to try that out. Commented Sep 20, 2013 at 21:12

2 Answers 2

2

So basically, you're looking to negate the need to match pp.ProjectID = p.ProjectID when the flag is set. You can do that right in the JOIN criteria:

JOIN Projects p
    ON pp.ProjectID = p.ProjectID OR p.AllowIndirectCustomers = 1
Sign up to request clarification or add additional context in comments.

1 Comment

Holy Edge Case, Batman! Didn't know you could do that with an OR in the JOIN. Learnt something new today. :)
0

Depending on the complexity of your tables, this might not work out too easily, but you could do a case statement on your bit column. Something like this:

select table1.id, table1.value, 
case table1.flag 
    when 1 then 
        table2.value
    else null
end as secondvalue
from table1
left join table2 on table1.id = table2.id

Here's a SQL Fiddle demo

1 Comment

I kinda wanna give you a +1 just for your username. But it sounds like the OP is looking to add rows rather than change the value of rows he already has.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.