Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 994792EE4E2 for ; Fri, 13 Jan 2012 10:26:23 -0400 (AST) Received: from nm5.bullet.mail.ac4.yahoo.com ([98.139.52.202]) by magus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Rli5B-0001BB-1G for pgsql-sql@postgresql.org; Fri, 13 Jan 2012 14:26:22 +0000 Received: from [98.139.52.192] by nm5.bullet.mail.ac4.yahoo.com with NNFMP; 13 Jan 2012 14:26:03 -0000 Received: from [98.139.52.130] by tm5.bullet.mail.ac4.yahoo.com with NNFMP; 13 Jan 2012 14:26:03 -0000 Received: from [127.0.0.1] by omp1013.mail.ac4.yahoo.com with NNFMP; 13 Jan 2012 14:26:03 -0000 X-Yahoo-Newman-Id: 872064.4927.bm@omp1013.mail.ac4.yahoo.com Received: (qmail 48820 invoked from network); 13 Jan 2012 14:26:03 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=kKFeexaP/zSIIcYjQexMzMQylQdXGZL4SLM2jHXdcY7tK4VsoE2IvbQWnztZuztbr+dXOP4+ZthEIm1xm6oNjE3LoRULPZa9hoAFU0B8Ma4h8opeBESvH/Z4RVyQPMGStTRO92l5eD6uz1C9IQGy5XrTA162JxQPWIIsf7LtBdw= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1326464763; bh=yaR3L5dZQibpBsNvfRxtfmVsh8qbbS+4yOla08pC5zM=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=gdzeNjMhZJzEEo2PGToFtQ9hskgNOuhrmfeIxqO6VOqOKrw/YRjNoGKAde70xtoUkeqA+WDSpi/mR1ejpNVahZ6e5ewdPpTRvXT9V0sTWxD/f+o0TmgRxIB8GDwD2pNXHa8ZcyTNea6GyhXhy+YTFf2hUGvoSyzj0NTUMbUq+xM= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: bYiJwzAVM1lOpV_mcKJtVDzJlDRHFQSH171IIFckPYH4adN o3oU_a5yoTEjjnzmfBEmT9Sd4eCvIKRWzyulX3J1JS_wb4qiYYNYJVJ2dPfO UMQ1sn5OK6Lm7gRwVkYPJZ1zl2Gg1WhNilzZppmPHoGdljTJxIBf.Wd0P0Hg OgyiF.fr4UcLCNISZD1mHgrpvbS9CPaNpdE41JS_Ei7oZg9Wi5C3HK0r2_vq Cu3Zd8ootgk8d0hRwyNC10lTlogeElPT7cUlohG8yb1BoN.8jWNrEwIg8PTO 5lV3La90ksy5rqMpVK3byrxbVVLCT_AKp6sdO4qn36i6ZEi6EKwTfKocyI.i mK3KlQ6gggWAR9cwxjL2jS_tWXuzLjXECC4hRJM3lNguTZ6K0YFi1mBeQ4ZS qRJoQmlkNOfaJo.X1lb03fTnX3OXylvMkawSqxEnNGw3bQOFuT9WCXEE4gWX p7waVbZFgW9bJmhGsWGDLEqgKtKOGDK0Nlhzk5PXQfSIRUNYB4QsK.7xY49e a7TLTF2XT8YD89Ggw9qxU1pILgQbMNT8- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp108-mob.biz.mail.ac4.yahoo.com with SMTP; 13 Jan 2012 06:26:03 -0800 PST References: <4F0FB38A.2020809@eglobalreach.net> In-Reply-To: <4F0FB38A.2020809@eglobalreach.net> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: 7bit Content-Type: multipart/alternative; boundary=Apple-Mail-8DEBF788-EE66-4D60-90ED-3B248176DDD5 Message-Id: <85B4919C-C7AE-45BE-B32B-52B5B4F409FD@yahoo.com> Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9A405) From: David Johnston Subject: Re: Query Problem... Left OuterJoin / Tagging Issue Date: Fri, 13 Jan 2012 09:26:03 -0500 To: John Tuliao X-Pg-Spam-Score: -2.0 (--) X-Archive-Number: 201201/54 X-Sequence-Number: 36225 --Apple-Mail-8DEBF788-EE66-4D60-90ED-3B248176DDD5 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On Jan 12, 2012, at 23:31, John Tuliao wrote: > Hi, >=20 > I've been working on this for quite awhile now and don't seem to get the p= roper query. >=20 > I have basically 4 tables.=20 >=20 > 1. Table john_test contains the numbers of the calls. > 2. Table john_country contains the country with prefix. > 3. Table john_clients contains the clients and their sub_id's=20 > (because a client usually has a subclient, [ex. highway-2, highway-3])= and their prefix. > 4. Table john_test contains the phone numbers. >=20 > --------------------------------------------------------------------------= ------------------------------------------------------ >=20 > select * from john_client_rate limit 3; =20 >=20 > name | country | cali | cana | callrate | dir_id | trans_id | s= vc_id | base | incr | client_id=20 > ----------+----------------+------+------+----------+--------+----------+-= -------+------+------+----------- > highway | Afghanistan | | | 0.6212 | 0 | 0 | = | 6 | 6 | 4 > highway | Albania | | | 0.3945 | 0 | 1 | = | 6 | 6 | 4 > highway | Bahamas | | | 0.0513 | 0 | 1 | = | 6 | 6 | 4 > (3 rows) >=20 > select * from john_country limit 3; >=20 > country | state | prefix | area_code=20 > ---------------+-------+----------+----------- > Afghanistan | | 93 |=20 > Aland Islands | | 35818120 |=20 > Albania | | 355 |=20 > (3 rows) >=20 >=20 > select * from john_clients limit 3; >=20 > id | client_id | sub_id | name | prefix | type =20 > ----+-----------+--------+----------+---------+-------- > 80 | 80 | 0 | highway | 71081 | client > 80 | 80 | 0 | highway | 7107011 | client > 80 | 80 | 0 | highway | 71091 | client > (3 rows) >=20 > select * from john_test limit 3; >=20 > client_id | name | phonenum | calledphonenum | pho= nenumtranslat | direction | duration=20 > ----------+---------+----------------------+-------------------------+----= --------------+-----------+---------- > 2 | highway | 83863011351927330133 | 20100147011351927330133 | = | outbound | 363 > 2 | highway | 83863011441179218126 | 1943011441179218126 | = | outbound | 83 > 2 | highway | 83863011441179218126 | 20100147011441179218126 | = | outbound | 32 > (3 rows) >=20 > --------------------------------------------------------------------------= ------------------------------------------------------ >=20 > What I want to do is to remove the prefix, and retain the number using the= following query: >=20 > select =20 > john_test.name,=20 > john_test.gwrxdcdn,=20 > john_test.duration as dur,=20 > john_client_rate.name as name2,=20 > john_client_rate.country,=20 > john_country.prefix,=20 > substring(john_test.gwrxdcdn from length(john_country.prefix)+1) as strip,= =20 > get_duration(john_test.duration::int,john_client_rate.base,john_client_rat= e.incr) as realdur=20 > from john_test=20 > left outer join john_client_rate=20 > on (prefix in=20 > (=20 > select prefix from john_client_rate=20 > where john_test.phonenum ~ ( '^' || john_country.prefix)=20 > order by length(john_country.prefix) desc limit '1' =20 > )=20 > )=20 > limit 20; If you have a select within the ON clause of a join it isn't really a join. = ON clauses should be simple expressions (almost always equality) between fi= elds on the two tables with AND/OR logic. >=20 > --------------------------------------------------------------------------= ------------------------------------------------------ >=20 > I have achieved this already, now I want to identify which country it's su= pposed to be for. > Problem is sometimes the "stripped" number that is retained shows: 8661234= 567 or 8889876543 > This would indicate that the call is already toll free without me being ab= le to identify the country.=20 > How can I get over this?=20 >=20 > Further, I am planning to use multiple joins since I have several tables a= nd so as to identify missing countries. On this questions which query is bet= ter? >=20 > Query 1: >=20 > Select table1.column,table2.column,table3.column from table1 left outer jo= in table 2 on (table1.column=3Dtable2.column) left outer join table3 on (tab= le2.column=3Dtable3.column) ; >=20 > or Query 2: >=20 > Select table1.column,table2.column,table3.column from table1,table2,table3= where [conditions] ; >=20 Query 1 is an outer join, query 2 is an inner join; totally different semant= ics so the question is more "which one will work" versus "which one is bette= r". Do you at least understand the difference? > Ultimately, I want to run one query that will satisfy these things and hel= p me insert into a table that will have it "TAGGED" properly with the right C= ountry, Client(name), prefix, and Rate for computation with Duration. >=20 > Hope you can reply to me asap. This is of urgent importance. Thank you and= any help would be greatly appreciated! >=20 > - JT Ignore this specific query for the moment and just figure out the various re= lationships between the tables. Once you have that the queries become much e= asier. Write out your desired output columns, with table prefixes, and mark whether= each on is optional or mandatory. Tables with optional fields are outer jo= ined to other tables, ideally those with only mandatory fields. The corresp= onding ON clauses should use simple equalities, though you may modify the th= e comparison values using functions.=20 It sounds like you need to take a step back and do some serious reading on S= QL basics, though I'll give you credit for at least trying and being somewha= t descriptive of your goal. David J.= --Apple-Mail-8DEBF788-EE66-4D60-90ED-3B248176DDD5 Content-Transfer-Encoding: 7bit Content-Type: text/html; charset=utf-8
On Jan 12, 2012, at 23:31, John Tuliao <jptuliao@eglobalreach.net> wrote:

Hi,

I've been working on this for quite awhile now and don't seem to get the proper query.

I have basically 4 tables.

1. Table john_test contains the numbers of the calls.
2. Table john_country contains the country with prefix.
3. Table john_clients contains the clients and their sub_id's
    (because a client usually has a subclient, [ex. highway-2, highway-3]) and their prefix.
4. Table john_test contains the phone numbers.

----------------------------------------------------------------
----------------------------------------------------------------

select * from john_client_rate limit 3; 

   name   |    country     | cali | cana | callrate | dir_id | trans_id | svc_id | base | incr | client_id
----------+----------------+------+------+----------+--------+----------+--------+------+------+-----------
 highway  | Afghanistan    |      |      |   0.6212 |      0 | 0        |        |    6 |    6 |         4
 highway  | Albania        |      |      |   0.3945 |      0 | 1        |        |    6 |    6 |         4
 highway  | Bahamas        |      |      |   0.0513 |      0 | 1        |        |    6 |    6 |         4
(3 rows)

select * from john_country limit 3;

    country    | state |  prefix  | area_code
---------------+-------+----------+-----------
 Afghanistan   |       | 93       |
 Aland Islands |       | 35818120 |
 Albania       |       | 355      |
(3 rows)


select * from john_clients limit 3;

 id | client_id | sub_id | name     | prefix  |  type 
----+-----------+--------+----------+---------+--------
 80 |        80 |      0 | highway  | 71081   | client
 80 |        80 |      0 |
highway  | 7107011 | client
 80 |        80 |      0 |
highway  | 71091   | client
(3 rows)

select * from john_test limit 3;

client_id |  name   |       phonenum       |     calledphonenum      | phonenumtranslat | direction | duration
----------+---------+----------------------+-------------------------+------------------+-----------+----------
        2 | highway | 83863011351927330133 | 20100147011351927330133 |                  | outbound  |      363
        2 | highway | 83863011441179218126 | 1943011441179218126     |                  | outbound  |       83
        2 | highway | 83863011441179218126 | 20100147011441179218126 |                  | outbound  |       32
(3 rows)

--------------------------------------------------------------------------------------------------------------------------------

What I want to do is to remove the prefix, and retain the number using the following query:

select 
john_test.name,
john_test.gwrxdcdn,
john_test.duration as dur,
john_client_rate.name as name2,
john_client_rate.country,
john_country.prefix,
substring(john_test.gwrxdcdn from length(john_country.prefix)+1) as strip,
get_duration(john_test.duration::int,john_client_rate.base,john_client_rate.incr) as realdur
from john_test
left outer join john_client_rate
on (prefix in
   (
      select prefix from john_client_rate
      where john_test.phonenum ~ ( '^' || john_country.prefix)
      order by length(john_country.prefix) desc limit '1'  
   )
   )
limit 20;

If you have a select within the ON clause of a join it isn't really a join.  ON clauses should be simple expressions (almost always equality) between fields on the two tables with AND/OR logic.


--------------------------------------------------------------------------------------------------------------------------------

I have achieved this already, now I want to identify which country it's supposed to be for.
Problem is sometimes the "stripped" number that is retained shows: 8661234567 or 8889876543
This would indicate that the call is already toll free without me being able to identify the country.
How can I get over this?

Further, I am planning to use multiple joins since I have several tables and so as to identify missing countries. On this questions which query is better?

Query 1:

Select table1.column,table2.column,table3.column from table1 left outer join table 2 on (table1.column=table2.column) left outer join table3 on (table2.column=table3.column) ;

or Query 2:

Select table1.column,table2.column,table3.column from table1,table2,table3 where [conditions] ;


Query 1 is an outer join, query 2 is an inner join; totally different semantics so the question is more "which one will work" versus "which one is better".  Do you at least understand the difference?

Ultimately, I want to run one query that will satisfy these things and help me insert into a table that will have it "TAGGED" properly with the right Country, Client(name), prefix, and Rate for computation with Duration.

Hope you can reply to me asap. This is of urgent importance. Thank you and any help would be greatly appreciated!

- JT


Ignore this specific query for the moment and just figure out the various relationships between the tables.  Once you have that the queries become much easier.

Write out your desired output columns, with table prefixes, and mark whether each on is optional or mandatory.  Tables with optional fields are outer joined to other tables, ideally those with only mandatory fields.  The corresponding ON clauses should use simple equalities, though you may modify the the comparison values using functions. 

It sounds like you need to take a step back and do some serious reading on SQL basics, though I'll give you credit for at least trying and being somewhat descriptive of your goal.

David J.
--Apple-Mail-8DEBF788-EE66-4D60-90ED-3B248176DDD5--