Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id EDC179FB255 for ; Thu, 19 Oct 2006 09:30:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 96653-05 for ; Thu, 19 Oct 2006 09:30:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.171]) by postgresql.org (Postfix) with ESMTP id B6B169FB235 for ; Thu, 19 Oct 2006 09:30:22 -0300 (ADT) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu3) with ESMTP (Nemesis), id 0MKxQS-1GaX2S1ACs-0005eM; Thu, 19 Oct 2006 14:30:20 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1GaX2R-0005hG-00 for pgsql-sql@postgresql.org; Thu, 19 Oct 2006 14:30:19 +0200 Date: Thu, 19 Oct 2006 14:30:19 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Grouping by day, limiting amounts Message-ID: <20061019123018.GC10803@a-kretschmer.de> References: <453766DB.6000408@telefor.hu> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <453766DB.6000408@telefor.hu> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! User-Agent: Mutt/1.5.9i X-Provags-ID: kundenserver.de abuse@kundenserver.de login:51ea9561fc3f6d9539a2acd743e4748c X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.33 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS X-Spam-Level: X-Archive-Number: 200610/176 X-Sequence-Number: 26565 am Thu, dem 19.10.2006, um 13:51:55 +0200 mailte Mezei Zoltán folgendes: > Hi, > > I didn't really know what subject I should give. > > I have a table like this one: > > 2006.10.01.    Bela    10 > 2006.10.01.    Aladar    9 > 2006.10.01.    Cecil    8 > 2006.10.01.    Dezso    7 > 2006.10.01.    Elemer    6 > 2006.10.02.    Bela    11 > 2006.10.02.    Aladar    10 > 2006.10.02.    Cecil    9 > 2006.10.02.    Dezso    8 > 2006.10.02.    Ferenc    7 > 2006.10.03.    Bela    6 > 2006.10.03.    Aladar    5 > 2006.10.03.    Cecil    4 > 2006.10.03.    Dezso    3 > 2006.10.03.    Jozef    2 > > The first column is a date, the second is a name, the third is the number of > votes that the name received on that day. > > I would like to select the 3 (or 10) names with the most votes for each day. > > Any suggestions on how can it be done easily? I would write a Set-returning function. First, select distinct all dates, and interate over this to select the names and number of votes for this date, ordered by the votes descending and limit this to 3 oder 10 or whatever. simplified: create function .... (IN c int, OUT d date, OUT n text, OUT x int) returns setof record ... declare temp_date date; rec record; begin for select into temp_date distinct date from table loop for select into rec name, votes from table where datum=temp_date order by 2 limit by $1 loop n := rec.name; ... return next ... HTH, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47215, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net