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
 Conditional LEFT JOIN

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-02-15 : 10:42:51
Hi All - I have the following two tables which I am joining ...

TABLE_1

CellNumber
4165551234
4165551235
4165551236

TABLE_2

CellNumber | AREA | DATE
4165551234 01 01/05/2012
4165551234 02 12/05/2011
4165551234 03 02/05/2012
4165551235 01 01/05/2012
4165551235 02 12/05/2011
4165551235 03 02/05/2012
4165551236 10 01/05/2012
4165551236 11 12/05/2011
4165551236 12 02/05/2012

I am doing a LEFT JOIN based on the CellPhone number. In the second table, CellNumber is duplicated. I ONLY want it to join based on the MOST RECENT date for that CELLNumber. How can I achieve this with the existing code?
[CODE

SELECT T1.Cell_Number, T2.AREA FROM TABLE_1 T1
LEFT JOIN TABLE_2 T2 ON T1.CellNumber = T2.Cell_Number
[/CODE]
I would like the output as follows:

CellNumber AREA
4165551234 03
4165551235 02
4165551236 12



webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-02-15 : 10:55:07
SELECT T1.Cell_Number, T2.AREA FROM TABLE_1 T1
left join
(select row_number() over (partition by CellNumber order by [DATE] DESC)as rnum,* from TABLE_2)as t2
ON T1.CellNumber = T2.Cell_Number and t2.rnum=1


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

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-15 : 11:11:32
SELECT * FROM TABLE_1 t1
WHERE EXISTS (SELECT * FROM TABLE_2 t2
WHERE t1.CellNumber = t2.CellNumber
GROUP BY CellNumber
HAVING t1.DATE = MAX(t2.DATE))

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-02-15 : 11:11:40
Worked like a charm, THANKS SO MUCH!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-15 : 11:54:06
Which one?

I wonder how each performs against the other

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-02-16 : 11:43:33
You're didn't work Brett, because t1.DATE does not exist.
DATE only exists in T2.

I appreciate you trying!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2012-02-16 : 11:49:50
my Bad..all you need is table 2 then..unless you want soemthhing else also



SELECT * FROM TABLE_2 t2a
WHERE EXISTS (SELECT * FROM TABLE_2 t2b
WHERE t2a.CellNumber = t2b.CellNumber
GROUP BY CellNumber
HAVING t2a.DATE = MAX(t2b.DATE))


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -