X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 0F15C9DC873 for ; Thu, 16 Mar 2006 02:05:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 89062-07 for ; Thu, 16 Mar 2006 02:05:01 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 00FEC9DC808 for ; Thu, 16 Mar 2006 02:04:59 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id k2G64wx4012934; Thu, 16 Mar 2006 01:04:58 -0500 (EST) To: Bryce Nesbitt cc: pgsql-sql@postgresql.org Subject: Re: group by function, make SQL cleaner? In-reply-to: <4418F52F.4070804@obviously.com> References: <4418F52F.4070804@obviously.com> Comments: In-reply-to Bryce Nesbitt message dated "Wed, 15 Mar 2006 21:18:40 -0800" Date: Thu, 16 Mar 2006 01:04:58 -0500 Message-ID: <12933.1142489098@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.113 required=5 tests=[AWL=0.113] X-Spam-Score: 0.113 X-Spam-Level: X-Archive-Number: 200603/228 X-Sequence-Number: 24574 Bryce Nesbitt writes: > SELECT date_trunc('day',endtime),count(*) > FROM eg_event where endtime >= '2006-02-01' and endtime < '2006-03-01' > GROUP BY date_trunc('day',endtime) > ORDER BY date_trunc('day',endtime); > Is there a way to eliminate the ugly repeated use of > date_trunc('day',endtime)? In this particular case you could say ... GROUP BY 1 ORDER BY 1; "ORDER BY n" as a reference to the n'th SELECT output column is in the SQL92 spec. (IIRC they removed it in SQL99, but we still support it, and I think most other DBMSes do too.) "GROUP BY n" is *not* in any version of the spec but we allow it anyway. I'm not sure how common that notation is. This does not work in any context except repeating a SELECT result expression in GROUP BY or ORDER BY. regards, tom lane