dbTalk Databases Forums  

Weird join...

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Weird join... in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Costin Manda
 
Posts: n/a

Default Weird join... - 07-12-2004 , 04:47 AM






I need to join two tables in postgres (or any other SQL) like this:
table 1 has a list of phone numbers
table 2 has a list of country prefixes (like 40 for romania 407 for romania
mobile, etc)

I want to join the tables so that a number like
407111111 in table 1
is joined ONLY with
407 in table 2

Can you please help?

Thank you


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Weird join... - 07-12-2004 , 09:37 AM






On Mon, Jul 12, 2004 at 12:47:41 +0300,
Costin Manda <costin (AT) interpoint (DOT) ro> wrote:
Quote:
I need to join two tables in postgres (or any other SQL) like this:
table 1 has a list of phone numbers
table 2 has a list of country prefixes (like 40 for romania 407 for romania
mobile, etc)

I want to join the tables so that a number like
407111111 in table 1
is joined ONLY with
407 in table 2

Can you please help?
You should keep the country prefixes as a separate column in the table with
the full numbers. It might also be useful to break out area/city code
from the local phone number.
You also might want to store both the IDD prefixes (there can be multiple
ones for some countries) and the NDD prefixes for the phone numbers.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #3  
Old   
Mischa Sandberg
 
Posts: n/a

Default Re: Weird join... - 07-12-2004 , 12:43 PM



Assuming:
table Phone(Number varchar(11))
table Country(Prefix varchar(11), Name varchar(99), ...)

select *
from Phone, Country
where Prefix = (select max(Prefix) from Country where Number like
Prefix+'%')

""Costin Manda"" <costin (AT) interpoint (DOT) ro> wrote

Quote:
I need to join two tables in postgres (or any other SQL) like this:
table 1 has a list of phone numbers
table 2 has a list of country prefixes (like 40 for romania 407 for
romania
mobile, etc)

I want to join the tables so that a number like
407111111 in table 1
is joined ONLY with
407 in table 2



Reply With Quote
  #4  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Weird join... - 07-13-2004 , 06:43 AM



On Tue, Jul 13, 2004 at 12:02:38 +0300,
Costin Manda <costin (AT) interpoint (DOT) ro> wrote:

Please keep responses posted to the list while discussing the problem
so that others can comment and learn from the discussion.

Quote:
Well, I don't have that option. I have the tables already and they are
continually changed by programs not under my control. I need to join them
like I said above.

If there is no SQL method to do it, then I guess I must do it with a
temporary table and another join, but I am wondering if it is a simpler way,
because another table and join would just slow things down.
If you know how to extract the country code from the full phone number
then you can use that expression/function in a join and it will do what
you want. You will probably want to make an index on that expression
or function.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.