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 12CD511F6E1A for ; Mon, 3 Apr 2006 18:59:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 97548-01 for ; Mon, 3 Apr 2006 18:59:03 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from outbound7.internet-mail-service.net (outbound7.internet-mail-service.net [216.240.47.198]) by postgresql.org (Postfix) with ESMTP id E29EF11F65DB for ; Mon, 3 Apr 2006 18:59:01 -0300 (ADT) Received: from idiom.com (idiom.com [216.240.32.1]) by outbound.internet-mail-service.net (Postfix) with ESMTP id D748E244013 for ; Mon, 3 Apr 2006 14:59:00 -0700 (PDT) Received: from [192.168.1.109] (adsl-69-107-132-69.dsl.pltn13.pacbell.net [69.107.132.69]) by idiom.com (8.12.11/8.12.11) with ESMTP id k33Lx0VI019266 for ; Mon, 3 Apr 2006 14:59:00 -0700 (PDT) (envelope-from bryce1@obviously.com) Message-ID: <44319A9C.9040800@obviously.com> Date: Mon, 03 Apr 2006 14:58:52 -0700 From: Bryce Nesbitt User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050921 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: group by function, make SQL cleaner? References: <4418F52F.4070804@obviously.com> <12933.1142489098@sss.pgh.pa.us> <4419A426.3030000@obviously.com> In-Reply-To: <4419A426.3030000@obviously.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.16 required=5 tests=[AWL=0.160] X-Spam-Score: 0.16 X-Spam-Level: X-Archive-Number: 200604/10 X-Sequence-Number: 24819 Bryce Nesbitt wrote: > Tom Lane wrote: >> 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. >> >> > Thanks. Markus Bertheau also supplied this solution: > SELECT enddate, count(*) FROM ( > SELECT date_trunc('day', endtime) AS enddate FROM eg_event WHERE > endtime >= '2006-01-01' and endtime < '2006-03-01') as foo > GROUP BY enddate > ORDER BY enddate > Hmm. Is there a way to specify the "n" column in a WHERE? demo=> select p_last_name,count(*) from xx_person group by p_last_name where 2 > 28; ERROR: syntax error at or near "where" at character 65 LINE 1: ...name,count(*) from eg_person group by p_last_name where '3' ... demo=> select p_last_name,count(*) from xx_person group by p_last_name order by 2 desc limit 6; p_last_name | count -------------+------- Smith | 44 Miller | 37 Lee | 35 Williams | 33 Johnson | 30 Jones | 28 (6 rows) -- ---- Visit http://www.obviously.com/