Skip site navigation (1) Skip section navigation (2)

Select last there dates

From: "Loredana Curugiu" <loredana(dot)curugiu(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Select last there dates
Date: 2007-06-21 08:18:13
Message-ID: 1c23c8e70706210118h6b9490e3s11e2a6ba15cdff6c@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novicepgsql-sql
Hello again,

I have the following two tables:

Table 1:
uid | phone_number |
-----+-------------------------------
   8 | +40741775621 |
   8 | +40741775622 |
   8 | +40741775623 |
   9 | +40741775621 |
   9 | +40741775622 |
   9 | +40741775623 |
 10 | +40741775621 |
 10 | +40741775622 |
 10 | +40741775623 |
   7 | +40741775621 |
   7 | +40741775622 |
   7 | +40741775623 |
 11 | +40741775621 |
 11 | +40741775622 |
 11 | +40741775623 |

Table2:

           uid | phone_number |             date
 ---------------+-----------------------+-------------------------------
              8 | +40741775621 | 2007-06-21 10:40:00+00
              8 | +40741775621 | 2007-05-21 10:40:00+00
              8 | +40741775621 | 2007-04-21 10:40:00+00
              8 | +40741775621 | 2007-03-21 10:40:00+00
              8 | +40741775621 | 2007-06-20 10:40:00+00
              8 | +40741775621 | 2007-06-19 10:40:00+00
              8 | +40741775621 | 2007-06-18 10:40:00+00
              8 | +40741775622 | 2007-06-16 10:40:00+00
              8 | +40741775622 | 2007-06-15 10:40:00+00
              7 | +40741775622 | 2007-06-21 05:54:13.646457+00
              7 | +40741775621 | 2007-06-21 05:54:21.134469+00


For each uid  column from table1 I have different values phone_number
column.

For each uid and phone_number columns from table2 I have different
values for date column.

My task is to create a query which for a given uid returns all values
for phone_number column from table1 and last three values of date
column from table2.

For example, if uid=8 the query should return:

 phone_number |    date
-----------------------+------------
 +40741775621 | 2007-06-21, 2007-06-20, 2007-06-19
 +40741775622 | 2007-06-16, 2007-06-15
 +40741775623 |

I created the query

    SELECT table1.phone_number,
                  TO_CHAR( table2.date, 'YYYY-MM-DD' ) AS date
       FROM ( SELECT * FROM table1 WHERE uid=8 ) table1
LEFT JOIN table2
            ON table1.uid=8
          AND table1.uid=table2.uid
          AND table1.phone_number=table2.phone_number

which returns.

 phone_number |    date
--------------+------------
 +40741775621 | 2007-06-18
 +40741775621 | 2007-06-19
 +40741775621 | 2007-06-20
 +40741775621 | 2007-03-21
 +40741775621 | 2007-04-21
 +40741775621 | 2007-05-21
 +40741775621 | 2007-06-21
 +40741775622 | 2007-06-15
 +40741775622 | 2007-06-16
 +40741775623 |

I don't know how to use this result for obtaining the result I need.
I was thinking to get the dates for the phone_number into a array
and then to use array_to string  function, but I don't know how to do it.
Any help, please?

Loredana

Responses

pgsql-novice by date

Next:From: Richard HuxtonDate: 2007-06-21 09:18:29
Subject: Re: Select last there dates
Previous:From: Scott MarloweDate: 2007-06-20 22:13:15
Subject: Re: [GENERAL] [PERFORM] Postgres VS Oracle

pgsql-sql by date

Next:From: Dani CastaƱosDate: 2007-06-21 09:10:02
Subject: Results per letter query
Previous:From: A. KretschmerDate: 2007-06-21 05:58:00
Subject: Re: Technique for turning time ranges into a graph

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group