Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Problem joining tables with max value from second

Author  Topic 

dejan88
Starting Member

7 Posts

Posted - 2012-03-07 : 08:13:49
I have two tables:
T1:

ID STATUS
1 NEW
2 OPEN
3 PENDING

T2:

ID VALUE DESCRIPTION
1 2 A1
1 20 A2
2 3 B1
2 30 B2


I would like to join this two tables with left outer join but from the second table to get only DESCRIPTION for max VALUE for that ID.

Result Table:
ID DESCRIPTION
1 A2
2 B2
3 null

My sql query is:

SELECT T1.ID, T2.DESCRIPTION
FROM T1
LEFT OUTER JOIN T2
ON T1.ID=T2.ID
WHERE T2.VALUE= (SELECT MAX(VALUE)
FROM T2
WHERE T2.ID=T1.ID)


but I do not have
ID 3 in my result which I do want although I did Left Outer Join. So what do I miss?
Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-07 : 08:35:02
try to add to the WHERE clause:
OR (T2.ID IS NULL)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

dejan88
Starting Member

7 Posts

Posted - 2012-03-07 : 09:00:35
Thanks
Go to Top of Page
   

- Advertisement -