From: Gerhard Dieringer <DieringG@eba-haus.de>
To: pgsql-sql@postgresql.org
To: " <Joseph Shraibman" <jks@selectacast.net>
Subject: Antw: aliases break my query
Date: Fri, 26 May 2000 08:54:10 +0200
Message-ID: <s92e3bbc.001@kopo001> (raw)
Joseph Shraibman wrote:
> These two queries are exactly alike. The first one uses aliases except
> for the order by. The second uses aliases also for the order by. The
> third uses whole names. The third has the behavior I want.
> Someone please tell me what I am doing wrong. I don't want to have to
> use whole names for my query.
> playpen=> select ta.a,ta.b,ta.c, (select count (tb.zz) where tb.yy =
> ta.a) from tablea ta, tableb tb order by tablea.a;
> playpen=> select ta.a,ta.b,ta.c, (select count (tb.zz) where tb.yy =
> ta.a) from tablea ta, tableb tb order by ta.a;
>playpen=> select tablea.a,tablea.b,tablea.c, (select count (tableb.zz)
> where tableb.yy = tablea.a) order by tablea.a;
I think what you actually want is an outer join:
SELECT tablea.a, tablea.b, tablea.c, Count(tableb.zz) AS zzcount
FROM tablea LEFT JOIN tableb ON tablea.a = tableb.yy
GROUP BY tablea.a, tablea.b, tablea.c;
that is not supported in postgreSQL 7.0, but can be simulated with
SELECT tablea.a, tablea.b, tablea.c, Count(tableb.zz) AS zzcount
FROM tablea INNER JOIN tableb ON tablea.a = tableb.yy
GROUP BY tablea.a, tablea.b, tablea.c
UNION
SELECT tablea.a, tablea.b, tablea.c, 0 AS zzcount
FROM tablea
WHERE tablea.a NOT IN (SELECT yy FROM tableb);
Gerhard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: DieringG@eba-haus.de, Joseph Shraibman" <jks@selectacast.net
Subject: Re: aliases break my query
In-Reply-To: <s92e3bbc.001@kopo001>
References: <s92e3bbc.001@kopo001>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox