Re: Problem using Subselect results

From: oheinz(at)stud(dot)fbi(dot)fh-darmstadt(dot)de
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Problem using Subselect results
Date: 2003-07-30 08:52:47
Message-ID: 1059555167.3f27875f7e3da@stud.fbi.fh-darmstadt.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I did try the following:

SELECT DISTINCT ON (two.two_id) two.two_value FROM one,two WHERE
(one.two_id=two.two_id
AND one.updatenr > two.updatenr) ORDER BY two.updatenr ASC;

I thought this would
a) order the result list by updatenr
b) return the first record only for records that are equal on the two.two_id
field
which would return exactly what I need - return the record with the

But what I get instead is "ERROR: SELECT DISTINCT ON expressions must match
initial ORDER BY expressions" *grrrmpfh*

But after a while playing around I noticed that the "initial" in that sentence
above is important - adding two.two_id as first argument to the ORDER BY does
not change the result but fulfill the requirements - the expressions don't have
to be excactly the same - just the initial argument.

My view which returns the (hopefully) correct results:

SELECT DISTINCT ON (two.two_id) two.two_value FROM one, two WHERE ((one.two_id
= two.two_id) AND (one.updatenr > two.updatenr)) ORDER BY two.two_id,
two.updatenr;

Next step is to include corresponding information from table three - now I need
subselects right? no way around with this DISTINCT and ORDER by stuff in it ...

Many thanks for your help so far,
Oliver

Quoting Oliver Heinz <oheinz(at)stud(dot)fbi(dot)fh-darmstadt(dot)de>:

> I'll try this tomorrow - combining DISTINCT ON (two.two_id) and sorting by
> two.updatenr could (should) have the desired effect - I never thought about
> using ORDER and DISTINCT that way.
>
> I'll report my success or failure...
>
> Thanks so far!
>
> Bye,
> Oliver

-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2003-07-30 09:06:39 Which cursor-related warnings should be errors?
Previous Message Anagha Joshi 2003-07-30 07:52:03 time precision.