I have a dataset from a complex query that I selected in the temp table. Let me call it #mydata p>
MyData is essentially a list of customer transactions. Each client could make several transactions
i.e.
ClientId TransactionId 1 123 1 234 1 564 2 897 3 714 5 850 5 963 7 325 8 912 8 375 8 640
Now for each row in this table, I want to find the value from another table by joining the client id.
The other table contains the rating for each client. And there can only be 1 โapprovedโ rating for each client. But there may be other ratings for the client in an unapproved state. And it may also be that for this client there is no rating yet. (Approved status identifier 5 - see below).
So, I am using the LEFT connection from mydataset to ClientRating
I want to get the same data as in #mydata, only with an extra column. I need as many lines as #mydata. If there is an approved customer rating, then put it in an additional column; if not, leave it blank
Everything I've tried so far doesn't work.
What am I doing wrong?
Without a connection, I get 2050 rows. With all the connections I tried, I get a different number:
--this returns 2050 rows select * from #mydata md --this returns 2111 rows select * from #mydata md LEFT JOIN ClientRating b on b.ClientId = md.ClientId AND (ClientRatingStatusid = 5) --this returns 2111 rows select * from #mydata md LEFT JOIN ClientRating b on b.ClientId = md.ClientId AND (ClientRatingStatusid = 5 OR ClientRatingStatusid IS NULL) --this returns 2111 rows select * from #mydata md LEFT outer JOIN ClientRating b on b.ClientId = md.ClientId AND (ClientRatingStatusid = 5 OR ClientRatingStatusid IS NULL) --this returns 2099 rows select * from #mydata md LEFT JOIN ClientRating b on b.ClientId = md.ClientId Where (ClientRatingStatusid = 5 OR ClientRatingStatusid IS NULL)
source share