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

Re: [SQL] JOIN

From: "Oliveiros Cristina" <oliveiros(dot)cristina(at)gmail(dot)com>
To: "Loredana Curugiu" <loredana(dot)curugiu(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] JOIN
Date: 2007-06-05 13:19:06
Message-ID: f54607780706050619u2e2c12dpcc3a4414370e0636@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novicepgsql-sql
Hey, Loredana.

Please advice me,
you need to sum for a certain pair (Theme, receiver) the number that appears
on count for every record whose date is in dates column, is this correct?
But in what record's dates column? On all of them? Or just the dates column
of the records that have that (Theme , Receiver) ?

Suppose I have
3| CRIS | rec1 | date1 | (date1,date2)
3| CRIS | rec1 | date2 | (date1,date3)

What would be your intended sum?
3 ? 6 ?
date2 is not on dates column for that record, but it is on the first...

Could you please show me an example of what would be the correct output for
ex for ,
CRIS   | +40741775622 ?
And For
LIA | +40741775621 ?

Thanx in advance

Best,
Oliveiros

2007/6/5, Loredana Curugiu <loredana(dot)curugiu(at)gmail(dot)com>:
>
> Any help, please?
>
> On 6/5/07, Loredana Curugiu <loredana(dot)curugiu(at)gmail(dot)com> wrote:
> >
> > Hi everybody,
> >
> > I have the following table:
> >
> > count | theme  |   receiver        |             date
> > |                                     dates
> >                                            |
> > -------+-----------+----------------------+------------------------------------+-------------------------------------------------------------------------------------------------------------------------+-------------------
> >
> >       2 | LIA      | +40741775621 | 2007-06-02 00:00:00+00 |
> > {2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07}
> >         |
> >       1 | LIA      | +40741775621 | 2007-06-04 00:00:00+00 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09}
> > |
> >       3 | CRIS   | +40741775622 | 2007-06-01 00:00:00+00 |
> > {2007-06-01,2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07}
> > |
> >       1 | CRIS   | +40741775622 | 2007-06-04 00:00:00+00 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09,2007-06-10}
> > |
> >       2 | LIA      | +40741775621 | 2007-06-03 00:00:00+00 |
> > {2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08}
> >        |
> >       1 | CRIS   | +40741775622 | 2007-06-04 00:00:00+00 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09,2007-06-10}
> > |
> >       1 | CRIS   | +40741775622 | 2007-06-03 00:00:00+00 |
> > {2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09}
> > |
> >       1 | CRIS   | +40741775622 | 2007-06-04 00:00:00+00 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09,2007-06-10}
> > |
> >       4 | LIA      | +40741775621 | 2007-06-01 00:00:00+00 |
> > {2007-06-01,2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06}
> > |
> >       1 | LIA      | +40741775621 | 2007-06-04 00:00:00+00 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09}
> > |
> >       1 | CRIS   | +40741775622 | 2007-06-02 00:00:00+00 |
> > {2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08}
> > |
> >
> > I want to add up the count column grouped by theme and receiver for the
> > dates included in the dates column.
> > So  I have the following query:
> >
> >        SELECT SUM(A.count),
> >                      A.theme,
> >                      A.receiver,
> >                      A.dates
> >           FROM my_table A
> > INNER JOIN my_table B
> >               ON A.theme=B.theme
> >             AND A.receiver=B.receiver
> >             AND A.date=ANY(B.dates)
> >  GROUP BY A.theme,A.receiver, A.dates;
> >
> > The result of the query is:
> >
> > sum | theme   |    receiver        |
> > dates
> >
> > -------+-----------+--------------+--------------------------------------------------------------------------------
> >      3 | CRIS   | +40741775622 |
> > {2007-06-01,2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07}
> >      2 | CRIS   | +40741775622 |
> > {2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08}
> >      3 | CRIS   | +40741775622 |
> > {2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09}
> >    18 | CRIS   | +40741775622 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09,2007-06-10}
> >      4 | LIA      | +40741775621 |
> > {2007-06-01,2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06}
> >      4 | LIA      | +40741775621 |
> > {2007-06-02,2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07}
> >      6 | LIA      | +40741775621 |
> > {2007-06-03,2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08}
> >    10 | LIA      | +40741775621 |
> > {2007-06-04,2007-06-05,2007-06-06,2007-06-07,2007-06-08,2007-06-09}
> >
> > The result is wrong. I don't know what it is wrong at my query.
> > Please help.
> >
> >
> > Best,
> >      Loredana
> >
> >
> >
> >
>


-- 
O QuĂȘ? SQL Server 2005 Express Edition? for
free?  easy-to-use??  lightweight???  and embeddable???  Isso deve ser uma
fortuna, homem!

In response to

  • Re: JOIN at 2007-06-05 11:26:13 from Loredana Curugiu

Responses

pgsql-novice by date

Next:From: Richard HuxtonDate: 2007-06-05 13:21:44
Subject: Re: JOIN
Previous:From: Loredana CurugiuDate: 2007-06-05 12:59:04
Subject: Re: [SQL] JOIN

pgsql-sql by date

Next:From: Richard HuxtonDate: 2007-06-05 13:21:44
Subject: Re: JOIN
Previous:From: Brad NicholsonDate: 2007-06-05 13:12:47
Subject: Re: the right time to vacuum database?

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