Re: Help with subselect (first time)

From: "Sean Davis" <sdavis2(at)mail(dot)nih(dot)gov>
To: "Rodolfo J(dot) Paiz" <rpaiz(at)simpaticus(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Help with subselect (first time)
Date: 2005-02-05 01:43:35
Message-ID: 000a01c50b24$2005b3e0$7d75f345@WATSON
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


----- Original Message -----
From: "Rodolfo J. Paiz" <rpaiz(at)simpaticus(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>
Sent: Friday, February 04, 2005 6:46 PM
Subject: Re: [NOVICE] Help with subselect (first time)

> On Thu, 2005-02-03 at 13:04 -0600, Rodolfo J. Paiz wrote:
>> I will guess in advance that I'm missing something obvious, but I
>> *think* I need a subselect here and I've never done those.
>
> Following up on this thread, I've decided that it's impossible to use a
> subselect for this purpose. Wrong tool. Now investigating joins...
>
> For simplicity's sake's, let's say I have a query which returns this:
>
> | month | days |
> |-----------|------|
> | 2005-01 | 31 |
> |-----------|------|
> | 2005-02 | 28 |
> |-----------|------|
> | 2005-03 | 31 |
> |-----------|------|
>
> And I have a second query which returns this:
>
> | month | flts |
> |-----------|------|
> | 2005-01 | 11 |
> |-----------|------|
> | 2005-03 | 8 |
> |-----------|------|
>
> Is there a simple way to join those two result sets into one? What I'd
> like to have is this:
>
> | month | days | flts |
> |-----------|------|------|
> | 2005-01 | 31 | 11 |
> |-----------|------|------|
> | 2005-02 | 28 | 0 |
> |-----------|------|------|
> | 2005-03 | 31 | 8 |
> |-----------|------|------|
>

Rodolfo,

If these are two tables, called A and B, then doing:

select B.month,days,flts from A,B where A.month=B.month;

will give you your table. If you need all "months", then using:

select B.month,days,flts from B left outer join A on A.month=B.month;

However, I doubt that the two outputs you show above are tables, so the two
queries here may not be what you want.

As for SQL, there are numerous websites (do a google search for SQL
tutorial) to learn how to do joins as well as many books, several of which
are online. The postgresql documentation has a tutorial section that
includes links to a couple of books.

Sean

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Rodolfo J. Paiz 2005-02-05 03:40:29 Re: Help with subselect (first time)
Previous Message Rodolfo J. Paiz 2005-02-04 23:46:27 Re: Help with subselect (first time)