SQLTeam.com | Weblogs | Forums

Help for the query

sql2008r2

#1

Hey everyone,

Can you help me with query that i can join with this table condition:
Table A:
|Name_Phone|
|A_0123123|
|B_0124124|

Table B:
|Name_Phone|ADRRESS|
|A_01230123|A|
|A_01230123|B|
|A_01230123|C|
|B_0124124|A|
|B_0124124|B|

and the result that i want is, i just want to join that always the last row of duplicate data, for example:

|Name_Phone|ADRRESS|
|A_01230123|C|
|B_0124124|B|

Need help


#2
; WITH CTE AS
(
	SELECT	[T_RowNumber] = ROW_NUMBER()
			OVER
			(
				PARTITION BY B.Name_Phone 
				ORDER BY B.Name_Phone, B.ADRRESS DESC
			),
		B.Name_Phone,
		B.ADRRESS
	FROM	[Table A] AS A
		JOIN [Table B] AS B
			 ON B.Name_Phone = A.Name_Phone
)
SELECT	Name_Phone,
	ADRRESS
FROM	CTE
WHERE	T_RowNumber = 1
ORDER BY Name_Phone