X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 1D0905E381D for ; Fri, 6 Aug 2004 02:22:39 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 53021-09 for ; Fri, 6 Aug 2004 05:22:35 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 407C95E381C for ; Fri, 6 Aug 2004 02:22:34 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 01720354D1; Thu, 5 Aug 2004 22:22:33 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id F4196350D4; Thu, 5 Aug 2004 22:22:33 -0700 (PDT) Date: Thu, 5 Aug 2004 22:22:33 -0700 (PDT) From: Stephan Szabo To: Ram Nathaniel Cc: pgsql-sql@postgresql.org Subject: Re: SQL syntax extentions - to put postgres ahead in the race In-Reply-To: Message-ID: <20040805220147.O92438@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/35 X-Sequence-Number: 18532 On Thu, 5 Aug 2004, Ram Nathaniel wrote: > > 1) The operator "of max": > suppose I have a table "grades" of 3 fields: class/student/grade where I > store many grades of many students of many classes. I want to get the > name of the highest scoring student in each class. Note that there may > be many students with the same grade, but for starters let's say there > is a primary key of class+grade. > > My query would now be: > select student from grades where class+'#'+grade in > ( > select class+'#'+max(grade) from grades group by class > ) a As a side note, I'd think that something like: select student from grades where (class,grade) in (select class, max(grade) from grades group by class); should avoid textual operations. I'm assuming the + above are meant to be concatenation (||). > The optimal would be to introduce a new operator "of max" that would be used as follows: > > select student of max(grade) from grades group by class PostgreSQL provides an extension called DISTINCT ON. Something like select distinct on (class) student from grades order by class, grade desc; should get you one arbitrary student with the highest grade in his or her class. If you want to order by the grades, I think you need a layer around it. If you don't care about the class order, you might consider making the class ordering desc as well to make it easier to use a multi-column index on (class,grade). > 2) aggregated concatenation: Theoretically, you should be able to do this right now in PostgreSQL with user defined aggregates (although you can't pass a second argument currently for the separator). I believe that an ordered subquery in FROM will currently allow you to get an ordered aggregate, or perhaps you'd have to turn off hash aggregation, but I think you should be able to get it to keep the ordering.