Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 590B74765CF for ; Thu, 23 May 2002 11:29:13 -0400 (EDT) Received: from ashtonwoodshomes.com (unknown [216.94.125.242]) by postgresql.org (Postfix) with ESMTP id B93684765F7 for ; Thu, 23 May 2002 10:53:35 -0400 (EDT) Received: from greatgulfhomes.com. (IDENT:root@[216.191.203.109]) by ashtonwoodshomes.com (8.11.6/8.9.3) with ESMTP id g4NEr1L12492 for ; Thu, 23 May 2002 10:53:01 -0400 Received: from terrynew2 (firewall.development.greatgulfhomes.com [216.191.203.106]) by greatgulfhomes.com. (8.11.6/8.11.6) with SMTP id g4NEoPt04970 for ; Thu, 23 May 2002 10:50:25 -0400 From: terry@greatgulfhomes.com To: "Postgres (E-mail)" Subject: DISTINCT and ORDER BY issue Date: Thu, 23 May 2002 10:49:07 -0400 Message-ID: <001401c20268$fdff0c40$2766f30a@development.greatgulfhomes.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 X-Archive-Number: 200205/16 X-Sequence-Number: 1374 I know what I am doing wrong: The order by needs to match the DISTINCT fields. Here's what I am doing: Get the 10 most recent applications user has used to put in their "recently used" menu. So I need to ORDER BY user_app_access_log.access_stamp DESC But I want each record distinct, eg if the last 5 hits were all for the same app, I want 1 record returned for that. So if I put the access_stamp field in the DISTINCT field set, then I go back to getting repeats of apps, hence not 10 unique app names... I wonder if there is a way to use an aggregate function to just return the first value, (ignoring the rest of the group), and use the group by clause on the app_name? Any ideas is appreciated. Here is my query/error message: Error while executing the query; ERROR: For SELECT DISTINCT, ORDER BY expressions must appear in target list SELECT DISTINCT applications.app_name, applications.app_long_name, applications.app_url, user_access.in_new_window FROM applications, user_access, user_app_use_log WHERE user_app_use_log.user_id = '1' AND user_app_use_log.app_name != 'index' AND user_access.app_name = user_app_use_log.app_name AND user_access.user_id = user_app_use_log.user_id AND user_access.division_id = 'GGH' AND applications.app_name = user_access.app_name ORDER BY user_app_use_log.access_stamp DESC LIMIT 10" Thanks in advance Terry Fielder Network Engineer Great Gulf Homes / Ashton Woods Homes terry@greatgulfhomes.com