pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@postgresql.org
Cc: pgsql-sql@postgresql.org
Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
Date: Thu, 27 Jan 2000 23:28:04 +0100 (CET)
Message-ID: <Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain> (raw)
In-Reply-To: <18389.948912054@sss.pgh.pa.us>

On 2000-01-26, Tom Lane mentioned:

> Peter Eisentraut <peter_e@gmx.net> writes:
> > Our documents say that DISTINCT ON is equivalent to GROUP BY. I still
> > don't see why that wouldn't be true. You can always rewrite
> > select distinct on a a,b from test
> > as
> > select a, xxx(b) from test group by a
> > where xxx is some aggregate function (presumably min or max).
> 
> Not really.  Look at Julian's example.  He can't rewrite as
> 
> 	select a, min(b), min(c) from test group by a
> 
> because the idea is to get the c that corresponds to the min b.

I see. I believe what you want is this:

select one.a, two.b, two.c
from
    (select a, min(b) as "min_b" from test group by a) as one,
    (select b, c from test) as two
where one."min_b" = two.b

Not sure if this is completely legal as it stands but at least the idea
would be to join the grouped select with the plain one to get the c
corresponding to the minimum b. But of course we don't offer that, so it's
distinct on until then. (It would really surprise me if the distinct on
functionality was not at all possible to emulate using SQL, since in my
experience it is fairly complete with regards to querying options at
least.)

-- 
Peter Eisentraut                  Sernanders väg 10:115
peter_e@gmx.net                   75262 Uppsala
http://yi.org/peter-e/            Sweden





view thread (36+ messages)  latest in thread

Message-ID: <Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain>
Permalink:  ../Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain/
Also on:    postgresql.org/message-id/Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain

 · 

reply

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: peter_e@gmx.net, tgl@sss.pgh.pa.us, pgsql-hackers@postgresql.org
  Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace
  In-Reply-To: <Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain>
  References: <18389.948912054@sss.pgh.pa.us> <Pine.LNX.4.21.0001271922100.356-100000@localhost.localdomain>

* 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