Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id B69D19FB326 for ; Sat, 30 Jun 2007 01:15:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 87476-03 for ; Sat, 30 Jun 2007 01:15:45 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from oxford.xeocode.com (unknown [87.127.95.196]) by postgresql.org (Postfix) with ESMTP id 0A2AF9FB2D4 for ; Sat, 30 Jun 2007 01:15:45 -0300 (ADT) Received: from localhost.enta.net ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.67) (envelope-from ) id 1I4UN5-0006Hj-P9; Sat, 30 Jun 2007 05:15:43 +0100 From: Gregory Stark To: "Andreas" Cc: "PostgresSQL list" Subject: Re: percentages of a column In-Reply-To: <4685BC84.2090106@gmx.net> (maps.on@gmx.net's message of "Sat, 30 Jun 2007 04:14:28 +0200") Organization: EnterpriseDB References: <4685BC84.2090106@gmx.net> X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.sql" 503) Date: Sat, 30 Jun 2007 05:15:43 +0100 Message-ID: <87ved6krgw.fsf@oxford.xeocode.com> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/245 X-Sequence-Number: 28735 "Andreas" writes: > Example: > > Fruit Count % > -------------------------- > Bananas 5 10% > Apples 15 30% > Oranges 30 60% select fruit_name, count(*), round(count(*)::numeric / (select count(*) from basket) * 100, 0)::text||'%' as "%" from basket group by fruit_name order by "%"; fruit_name | count | % ------------+-------+----- Bananas | 5 | 10% Apples | 15 | 30% Oranges | 30 | 60% (3 rows) -- Gregory Stark EnterpriseDB http://www.enterprisedb.com