I will try to make my question sound as unconfusing as possible. I appologize in advance for any wording mistakes as I try to phrase my question as best as I can:
Using T-SQL I need to write a join statement that gets me all the results that have a match in table A and table B
AND (!)
another join statement (or a continuation of the first join) that returns all the results from table A that DID NOT have a match in table B, BUT in this second result set I need to have one of the columns set to “N/A” to identify the records that didn’t have a match.
In other words, I need something that would return everything in table A but would also identify the rows that weren’t matched in B. That information is then used in a report.
Here is what I have so far:
I have the first part done:
LEFT OUTER JOIN dbo.chart B
ON B.UserName = A.user_name
That gets me the matching records and just the matching records
I tried adding this second join:
JOIN dbo.chart
ON NOT EXISTS (select * from B.UserName = A.user_name)
Hoping it would get me the non-matching records (I was planning to then use REPLACE on the column of interest to label that column “N/A”) but there is something clearly wrong with my synthax as that generates exceptions.
My question is what do I need to change to get me the results you need. I do know that I need to have at least one join as I have other part of the query to work with. I just don’t know if I need to have that one join return both sets of data of I actually do need a second one just for the non-matching records.
Hope this wasn’t too confusing. Any help would be greatly appreciated.
Thank you!
Update: I would just like to emphasize that the reason I considered using a second join instead of getting all of the results at once is because I need to correctly identify and label those rows that weren’t matched within everything that I get back.
Sample data:
N/A instead of TableBName:
Result:
Extra column for N/A:
Result: