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 C4792D1D52E for ; Tue, 3 Feb 2004 03:14:19 +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 11315-08 for ; Mon, 2 Feb 2004 23:14:21 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 6F0E8D1D85F for ; Mon, 2 Feb 2004 23:14:17 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i133E2K4010685; Mon, 2 Feb 2004 22:14:03 -0500 (EST) To: Terry Lee Tucker Cc: pgsql-sql@postgresql.org Subject: Re: Novice SQL Question In-reply-to: <200402021738.06828.terry@esc1.com> References: <200402021738.06828.terry@esc1.com> Comments: In-reply-to Terry Lee Tucker message dated "Mon, 02 Feb 2004 17:38:06 -0500" Date: Mon, 02 Feb 2004 22:14:02 -0500 Message-ID: <10684.1075778042@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/25 X-Sequence-Number: 16716 Terry Lee Tucker writes: > If I put this where I thought it should go as in: > select distinct event_code,level from logs join stat on (stat.prime is not > null) where order_num = 130680 order by event_date,event_time,event_secs; > I get the following error: > ERROR: For SELECT DISTINCT, ORDER BY expressions must appear in target list Right. Think about what SELECT DISTINCT does: it combines all rows with the same values of event_code and level into a single row. The group of rows with a particular pair of event_code/level might contain many different values of event_date etc. Which of these values should be used to sort the combined row? The result just isn't well-defined in general. You need to alter the query so that it completely defines the result you want. One way to do that is suggested by the error message: add the ORDER BY columns into the DISTINCT list. But that's not necessarily the way that will get the result you want. regards, tom lane