Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 05F757412C2 for ; Sat, 10 Dec 2011 18:27:22 -0400 (AST) Received: from out2.mail.amis.hr ([188.129.0.5]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RZVO5-00011I-7i for pgsql-sql@postgresql.org; Sat, 10 Dec 2011 22:27:23 +0000 Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out2.mail.amis.hr (Postfix) with ESMTP id DD14E1FC7C for ; Sat, 10 Dec 2011 23:27:06 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id pBAMMrkd005048 for ; Sat, 10 Dec 2011 23:27:06 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id 11kr3vr3ej-1 for ; Sat, 10 Dec 2011 23:27:06 +0100 A-Original-Client-UID: 188.129.105.225 splivalo Received: from [192.168.10.1] (cpe-188-129-105-225.dynamic.amis.hr [188.129.105.225]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id 6985A270 for ; Sat, 10 Dec 2011 23:27:06 +0100 (CET) Message-ID: <4EE3DC17.3010804@megafon.hr> Date: Sat, 10 Dec 2011 23:24:23 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Subselects to Joins? Or: how to design phone calls database Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.5.7110, 1.0.211, 0.0.0000 definitions=2011-12-10_06:2011-12-09, 2011-12-10, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=1 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1012030000 definitions=main-1112100266 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201112/43 X-Sequence-Number: 36104 I have a table called 'calls' which holds 'call detail records'. Let's assume the table looks like this: CREATE TABLE cdr ( call_id serial, phone_number text ); And I have a table with country call prefixes, that looks like this: CREATE TABLE prefixes ( prefix text, country text ); And now some test data: INSERT INTO prefixes VALUES ('1', 'USA'); INSERT INTO prefixes VALUES ('44', 'UK'); INSERT INTO prefixes VALUES ('385', 'Croatia'); INSERT INTO prefixes VALUES ('387', 'Bosnia'); INSERT INTO prefixes VALUES ('64', 'New Zeland'); INSERT INTO prefixes VALUES ('642', 'New Zeland Mobile'); INSERT INTO calls VALUES (1, '11952134451'); INSERT INTO calls VALUES (2, '448789921342'); INSERT INTO calls VALUES (3, '385914242232'); INSERT INTO calls VALUES (4, '385914242232'); INSERT INTO calls VALUES (5, '645122231241'); INSERT INTO calls VALUES (6, '444122523421'); INSERT INTO calls VALUES (7, '64212125452'); INSERT INTO calls VALUES (8, '1837371211'); INSERT INTO calls VALUES (9, '11952134451'); INSERT INTO calls VALUES (10, '448789921342'); INSERT INTO calls VALUES (11, '385914242232'); INSERT INTO calls VALUES (12, '385914242232'); INSERT INTO calls VALUES (13, '645122231241'); INSERT INTO calls VALUES (14, '4441232523421'); INSERT INTO calls VALUES (15, '64112125452'); INSERT INTO calls VALUES (16, '1837371211'); Now, if I want to have a 'join' between those two tables, here is what I am doing right now: SELECT call_id, phone_number, (SELECT country FROM prefixes WHERE calls.phone_number LIKE prefix || '%' ORDER BY length(prefix) DESC LIMIT 1 ) AS country FROM calls; Is there a way I could use join here? I can do something like: SELECT ... FROM calls JOIN prefixes ON calls.phone_number LIKE prefix || '%' but I'd get duplicate rows there (for instance, for New Zeland calls, from my test data). Or should I add 'prefix' field to the calls table, and then do a inner join with prefixes table? Mario