Re: What can I use as a [non-aggregate] minimum function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Paul Wehr <postgresql(at)industrialsoftworks(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: What can I use as a [non-aggregate] minimum function
Date: 2001-12-10 23:07:02
Message-ID: 28156.1008025622@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"=?iso-8859-1?Q?Paul_Wehr?=" <postgresql(at)industrialsoftworks(dot)com> writes:
> I need to find the minimum of dates in a number of tables, but "min(date)"
> is, of course, an aggregate function. For example:

> select key, min(a.date, b.date, c.date) as first_date
> from table_a a, table_b b, table_c c
> where a.key=b.key and a.key=c.key

Does that really express the computation you want, ie produce a result
only for key values that occur in all three tables?

I was going to suggest

select key, min(date) as first_date from
(select key, date from table_a
union all
select key, date from table_b
union all
select key, date from table_c) subsel
group by key;

but it occurs to me that this produces different results, ie, it will
include key values that only occur in one or two of the tables ...

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Gould 2001-12-10 23:08:37 Re: Need SQL help, I'm stuck.
Previous Message Glen Eustace 2001-12-10 22:39:22 Re: Weird problem - possibly a bug.