Re: Help with subselect (first time) - Mailing list pgsql-novice

From Sean Davis
Subject Re: Help with subselect (first time)
Date
Msg-id 000a01c50b24$2005b3e0$7d75f345@WATSON
Whole thread Raw
In response to Help with subselect (first time)  ("Rodolfo J. Paiz" <rpaiz@simpaticus.com>)
Responses Re: Help with subselect (first time)
List pgsql-novice
----- Original Message -----
From: "Rodolfo J. Paiz" <rpaiz@simpaticus.com>
To: <pgsql-novice@postgresql.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




pgsql-novice by date:

Previous
From: "Rodolfo J. Paiz"
Date:
Subject: Re: Help with subselect (first time)
Next
From: "Rodolfo J. Paiz"
Date:
Subject: Re: Help with subselect (first time)