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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Complicated conditional join
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

17 Posts

Posted - 08/06/2010 :  20:52:46  Show Profile  Reply with Quote
Hi all

I have big list of international phone codes and a smaller rate sheet from a phone company.
I need to assign rates to my list. The difficulty is that our lists are different.
In the following example I have 4 codes in my list and only 2 codes in a phone company's list.
code 4478 has to get the rate $0.01 and code 44787210 has to get the rate $0.02

drop table #my_list
drop table #phone_company_list

create table #my_list
(cc_prefix numeric,
CityCountry varchar(200))
create table #phone_company_list
(cc_prefix numeric,
CityCountry varchar(200),
rate decimal(10,5))

insert into #my_list values (44,'UK DEFAULT')
insert into #my_list values (4478,'UK MMO2')
insert into #my_list values (4478721,'UNITED KINGDOM MMO2 MOBILE')
insert into #my_list values (44787210,'UNITED KINGDOM MMO2 MOBILE')

insert into #phone_company_list values (44,'UK DEFAULT',0.01)
insert into #phone_company_list values (4478721,'UNITED KINGDOM MMO2 MOBILE',0.02)

select * from #my_list
select * from #phone_company_list

In (Som, Ni, Yak)

17689 Posts

Posted - 08/06/2010 :  23:47:05  Show Profile  Reply with Quote
select 	* 
from 	#my_list m
	cross apply
		select	top 1 rate
		from	#phone_company_list p	
		where	convert(varchar(10), m.cc_prefix)	like convert(varchar(10), p.cc_prefix) + '%'
		order by cc_prefix desc
	) r

Time is always against us

Go to Top of Page

Starting Member

17 Posts

Posted - 08/09/2010 :  19:45:46  Show Profile  Reply with Quote
Thank you very much.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000