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 |
|
jlyles
Starting Member
2 Posts |
Posted - 2009-06-16 : 18:03:06
|
Here's the scenario. I've got two tables, one with quite a bit of data in it, and one with some additional contact data. I'll refer to the first table as DataTable and the second as ExtraDataTable. They look something like thisDataTablenum title email phone1 title1 bob@aol.com 555-555-12342 title2 jim@aol.com NULL3 title3 steve@aol.com NULL4 title4 tim@aol.com NULL5 title5 james@aol.com 555-555-2345ExtraDataTableemail phone frequencysue@aol.com 555-555-5556 2sue@aol.com 555-555-5555 1jim@aol.com 555-123-1234 2jim@aol.com 555-123-2345 1jim@aol.com 555-123-3456 1steve@aol.com 555-999-9999 2tim@aol.com 555-111-1111 1tim@aol.com 555-222-2222 2tim@aol.com 555-333-3333 3 As you can see, ExtraDataTable, holds additional phone numbers and their frequency, along with a corrosponding email address.I'm trying to develop a query that will return num, title, email, and phone from DataTable if the record has a phone. If the particular record doesnt have a phone, I need it to pull the phone number from ExtraDataTable that has the highest frequency. So the result would look like this.Resultnum title email phone1 title1 bob@aol.com 555-555-12342 title2 jim@aol.com 555-123-12343 title3 steve@aol.com 555-999-99994 title4 tim@aol.com 555-333-33335 title5 james@aol.com 555-555-2345 I've spent quite a bit of time trying to figure this out and it doesn't even seem like I'm headed in the right direction, so if anyone can give me any tips or what not, I would greatly appreciate it. |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2009-06-16 : 19:28:33
|
| [code]select a.num, a.title, a.email, coalesce(a.phone, b.phone) as phonefrom DataTable aleft join(select email, phonefrom(select email, phone, row_number() over (partition by email order by frequency desc) as rnfrom ExtraDataTable) where rn = 1) b on a.email = b.email [/code] |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-06-17 : 06:01:03
|
| [code]small modification to singularity codeselect a.num, a.title, a.email, coalesce(a.phone, b.phone) as phonefrom DataTable aleft join(select email, phone, row_number() over (partition by email order by frequency desc) as rnfrom ExtraDataTable) b on a.email = b.emailand rn = 1[/code] |
 |
|
|
jlyles
Starting Member
2 Posts |
Posted - 2009-06-18 : 16:29:03
|
| Thank you two for the quick replies! I did some research on the code, and the coalesce and ranking functions were exactly what I needed and the script works wonderfully. Recently coming from SQL 2000, I had no idea about the Rank and Partition features so it was nice to see the solution was so simple. Thanks again guys! |
 |
|
|
saran_d28
Starting Member
36 Posts |
Posted - 2009-06-19 : 05:30:12
|
| In SQL 2000u can use:select num, title, email, coalesce(main.phone, extra.phone)from datatable dataleft outer join(Select * from extradatatable ajoin (select email, max(frequency) from extratable) bon a.mail = b.mailand a.frequency = b.frequency) extraon data.email = extra.email |
 |
|
|
|
|
|
|
|