Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id A191E633DAC for ; Fri, 11 Dec 2009 11:43:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 05336-09 for ; Fri, 11 Dec 2009 15:42:58 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 7AA6C634378 for ; Fri, 11 Dec 2009 11:40:39 -0400 (AST) Received: from list by lo.gmane.org with local (Exim 4.50) id 1NJ7bg-0006ZR-6j for pgsql-sql@postgresql.org; Fri, 11 Dec 2009 16:40:36 +0100 Received: from srv.protecting.net ([212.126.218.242]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Dec 2009 16:40:36 +0100 Received: from hari.fuchs by srv.protecting.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 11 Dec 2009 16:40:36 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: Window function trouble Date: Fri, 11 Dec 2009 16:40:13 +0100 Lines: 57 Message-ID: References: <25983.1260468559@sss.pgh.pa.us> <28855.1260486487@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: srv.protecting.net X-Archive: encrypt User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:dBSL41SjVA8y11HPaDY8J5MEjg0= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200912/36 X-Sequence-Number: 33474 In article <28855.1260486487@sss.pgh.pa.us>, Tom Lane writes: > Harald Fuchs writes: >> That being said, I still think that PostgreSQL could do better - how >> about naming expression columns so that they are distinct from column >> names? > Even though the rules we use are pretty arbitrary, I'm hesitant to make > changes in them; it seems more likely to break existing applications > than to do anyone any good. Well, it would be far less confusing. Here's a self-contained example: CREATE TABLE t1 ( id serial NOT NULL, adr text NOT NULL, usr text NOT NULL, PRIMARY KEY (id) ); COPY t1 (adr, usr) FROM stdin DELIMITER '|'; a1|u1_1 a2|u2_1 a2|u2_2 a2|u2_2 a3|u3_1 a3|u3_2 a3|u3_2 a3|u3_3 a3|u3_3 a3|u3_3 \. SELECT CASE lag(adr) OVER (ORDER BY adr, usr) WHEN adr THEN NULL ELSE adr END, usr, count(*) FROM t1 GROUP BY adr, usr ORDER BY adr, usr; The result set is: adr | usr | count -----+------+------- a1 | u1_1 | 1 a2 | u2_1 | 1 a3 | u3_1 | 1 | u2_2 | 2 | u3_2 | 2 | u3_3 | 3 This means IMHO that "GROUP BY adr, usr" groups by the column named "adr", whereas "ORDER BY adr, usr" orders by the unnamed CASE expression which happens to "hide" the column name without warning.