X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5595ED1E0C7 for ; Thu, 19 Feb 2004 18:08:59 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 55169-10 for ; Thu, 19 Feb 2004 14:08:55 -0400 (AST) Received: from diver.highboard.com (ns5.highboard.com [207.44.244.101]) by svr1.postgresql.org (Postfix) with ESMTP id 98B08D1E0F6 for ; Thu, 19 Feb 2004 14:08:53 -0400 (AST) Received: from roc-66-67-208-52.rochester.rr.com ([66.67.208.52] helo=swimmer) by diver.highboard.com with asmtp (Exim 4.24) id 1Atsbb-0004YU-CX for pgsql-sql@postgresql.org; Thu, 19 Feb 2004 13:08:59 -0500 Reply-To: From: "Jeremy Smith" To: Subject: DISTINCT ON troubles Date: Thu, 19 Feb 2004 13:10:30 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) In-Reply-To: Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - diver.highboard.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - highboard.com X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/279 X-Sequence-Number: 16969 Hi, 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, Jeremy