0

I have the following tables:

Specialty

SpecialtyId Name IsEnabled

SpecialtyReport

UserId SpecialtyName ReportsRead

I am trying to figure out how to select all the records for a specific user from SpecialtyReport Table and then whatever Name is missing, populate it from Specialty Table and all other values can be null.

What I have tried is this:

SELECT s.Name, t.UserId, t.ReportsRead
FROM Specialty s
LEFT OUTER JOIN SpecialtyReport t ON s.Name = t.SpecialtyName
WHERE t.UserId = 1

However, the result that I get is same as if I did Inner join. The Specialty Table has about 10 rows and all with unique names, where as the SpeialtyReport Table has 3 rows with that User Id.

I can't figure out why the outer left join won't populate missing names. The end result I get from above query is just 3 rows.

EDIT:

The SpecialtyReport Table is made up, it comes from a subquery. I did a test where I created the actual table and my query works on it, but it doesn't work if I replace the table with results from a subquery. Why is that?

1
  • can you give sample data and expected output? Commented Feb 21, 2018 at 19:00

1 Answer 1

1

You should write UserId predicate at join part

SELECT s.Name, t.UserId, t.ReportsRead
FROM Specialty s
LEFT OUTER JOIN SpecialtyReport t ON s.Name = t.SpecialtyName
      and  t.UserId = 1
Sign up to request clarification or add additional context in comments.

4 Comments

How does this differ from my query? I'm sorry I can't seem to spot the difference.
I deleted the where part and moved UserId = 1 predicate to join part with and
use ISNULL(userId,'noid')
Yes, it is possible. You can use ISNULL or COALESCE functions. for example ISNULL(t.UserId,'')

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.