pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christoph Haller <ch@rodos.fzk.de>
To: pgsql-sql@postgresql.org
Cc: jkwast@freeler.nl
Subject: Re: howto/min values
Date: Wed, 19 Mar 2003 14:54:03 +0100
Message-ID: <3E78767B.47CA0F73@rodos.fzk.de> (raw)

> My postings seem to double on me.

It's standard procedure to reply twice, one to the list
and one to the sender, because you don't need to
subscribe to the list to get the answers. But if you are
subscribed and ask a question you usually get two.

>
> Now this works. So going to the next question, if distance is a
calculated =
> field how can I use it in the subquery. How do you add another alias
in:
>
> select p.points, p.id, (x-x) as distance=20
> from table as p
> where distance =3D (select min(distance)=20
>                             from table as p1
>                             where p.points=3Dp1points)
> order by points, id
> =20
> This of course doesn't work, because of the referring to the
calculated fie=
> ld wich is not actually a part of the table. Can you do this somehow
or is =
> it impossible?
>
No, not at all. Something like

select p.points, p.id, p.distance from
(select points, id, (x-x) as distance from table) as p
where p.distance = (select min(p1.distance)
>                             from table as p1
>                             where p.points=p1.points)
> order by p.points, p.id ;

should do the trick.
The FROM clause allows any kind of subqueries within parentheses.

Regards, Christoph






view thread (3+ messages)  latest in thread

Message-ID: <3E78767B.47CA0F73@rodos.fzk.de>
Permalink:  ../3E78767B.47CA0F73@rodos.fzk.de/
Also on:    postgresql.org/message-id/3E78767B.47CA0F73@rodos.fzk.de

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ch@rodos.fzk.de, jkwast@freeler.nl
  Subject: Re: howto/min values
  In-Reply-To: <3E78767B.47CA0F73@rodos.fzk.de>

* 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