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.
| Author |
Topic |
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-09-19 : 10:12:35
|
| hi there - I have the following tables (I've simplified it):Table 1 (T1):TEL_NUMBER ID5145551234 AB4CA9055559978 B00FA4165554567 FD4717805551237 157D9Table 2 (T2)TEL_NUMBER REGION5145551234 PQ5145551234 MTL5145551234 PQ9055559978 GTA9055559978 ACTON9055559978 BRADFORD4165554567 GTA4165554567 TORONTO7805551237 WEST7805551237 ALBERTA7805551237 CALGARYI'm trying join the tables together by the Telephone number. However the second table has multiple instances of the Telephone number (the "joining" value). The Telephone_num values are duplicated because there might be 3-4 different regions for each tel number.I want the query to pull display/join based on the FIRST region it finds, and that's it.How can I eliminate the duplicates from the result set? THANKS !!!!!!!!!!My existing query:SELECT DISTINCT T1.TEL_NUMBER, T2.REGION FROM T1LEFT JOIN T2 ON T1.TEL_NUMBER=T2.TEL_NUMBERDesired Result Set:5145551234 PQ9055559978 GTA4165554567 GTA7805551237 WEST |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2011-09-19 : 10:15:50
|
| Hello,Have a look at the 'DISTINCT' commmand.HTH.EDIT. When requirements change, so do appropriate responses... :) |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2011-09-19 : 10:33:32
|
quote: Originally posted by funk.phenomena I want the query to pull display/join based on the FIRST region it finds, and that's it.How can I eliminate the duplicates from the result set? THANKS !!!!!!!!!!
Hello,You mention that are looking for the 'first region it finds'. What is your ordering mechanism? Do you have a field you wish to sort by to pull the first one in the series?If so, can you please specify the ordering column(s)? |
 |
|
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-09-19 : 12:54:11
|
| There's no specific ordering requirements. Just the first record based on the record location in the table.For example, the First record, It doesn't matter if it pulls up "PQ" or "MTL". Just as long as it pulls only one. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2011-09-19 : 12:57:51
|
quote: Originally posted by funk.phenomena There's no specific ordering requirements. Just the first record based on the record location in the table.For example, the First record, It doesn't matter if it pulls up "PQ" or "MTL". Just as long as it pulls only one.
I see, how about something like this then?;SELECT t1.TEL_NUMBER, MIN(t2.REGION) AS REGIONFROM Table1 t1LEFT JOIN Table2 t2 ON t2.TEL_NUMBER = t1.TEL_NUMBERGROUP BY t1.TEL_NUMBER |
 |
|
|
funk.phenomena
Posting Yak Master
121 Posts |
Posted - 2011-09-19 : 14:04:44
|
| Thanks for your suggestion. It doesn't work because the region is still duplicated in the second table. 7805551237 WEST7805551237 ALBERTA7805551237 WEST7805551237 WESTWhen I use the MIN (or MAX) function, it will still generate a result set with multiple Telephone numbers:7805551237 WEST7805551237 ALBERTAAny other Ideas? |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2011-09-19 : 14:15:46
|
quote: Originally posted by funk.phenomena Thanks for your suggestion. It doesn't work because the region is still duplicated in the second table. 7805551237 WEST7805551237 ALBERTA7805551237 WEST7805551237 WESTWhen I use the MIN (or MAX) function, it will still generate a result set with multiple Telephone numbers:7805551237 WEST7805551237 ALBERTAAny other Ideas?
When testing such a query, I do not see such a result.create table #t1 (TEL_NUMBER varchar(20), ID varchar(20))insert #t1select '5145551234','AB4CA'union all select '9055559978','B00FA'union all select '4165554567','FD471'union all select '7805551237','157D9'create table #t2 (TEL_NUMBER varchar(20), REGION varchar(20))insert #t2select '5145551234','PQ'union all select '5145551234','MTL'union all select '5145551234','PQ'union all select '9055559978','GTA'union all select '9055559978','ACTON'union all select '9055559978','BRADFORD'union all select '4165554567','GTA'union all select '4165554567','TORONTO'union all select '7805551237','WEST'union all select '7805551237','WEST'union all select '7805551237','WEST'union all select '7805551237','WEST'union all select '7805551237','ALBERTA'union all select '7805551237','CALGARY'SELECT t1.TEL_NUMBER, MIN(t2.REGION) AS REGIONFROM #t1 t1LEFT JOIN #t2 t2 ON t2.TEL_NUMBER = t1.TEL_NUMBERGROUP BY t1.TEL_NUMBERdrop table #t1, #t2 Perhaps you have other characters in your phone numbers which are making them distinct? I cannot tell.Sorry I could not be of more help for you. I hope you find a solution. |
 |
|
|
|
|
|
|
|