Skip site navigation (1) Skip section navigation (2)

DISTINCT ON troubles

From: "Jeremy Smith" <jer(at)highboard(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: DISTINCT ON troubles
Date: 2004-02-19 18:10:30
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql

I have a query that reads:

SELECT DISTINCT ON (messageboard.threadid) messageboard.threadid,
messageboard.topic, owner.ownerid, owner.username FROM messageboard, owner
WHERE messageboard.ownerid=owner.ownerid AND messageboard.leagueid =
'$leagueid' ORDER BY messageboard.messageid DESC LIMIT $entries_on_page
OFFSET $beginThread"

The purpose of this query is to retrieve unique threadid's in the order of
the most recent posts that have been made to each thread.  When I use this
query I get an error that:

"SELECT DISTINCT ON expressions must match initial ORDER BY expressions".

Of course, if I put ORDER BY threadid in the query it would order it in the
order that the thread was created, not in the last post made.

I have tried using GROUP BY threadID, I get a similar order.

Am I just approaching this all wrong and need to create a temporary table
and draw from that, or is there a way to salvage this query?

Thanks so much,

In response to


pgsql-sql by date

Next:From: eleinDate: 2004-02-19 18:55:05
Subject: Re: CHAR(n) always trims trailing spaces in 7.4
Previous:From: Philippe LangDate: 2004-02-19 17:28:49
Subject: Re: crosstabs

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group