Re: percentages of a column

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Andreas" <maps(dot)on(at)gmx(dot)net>
Cc: "PostgresSQL list" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: percentages of a column
Date: 2007-06-30 04:15:43
Message-ID: 87ved6krgw.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Andreas" <maps(dot)on(at)gmx(dot)net> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message dBHS Jakarta 2007-07-01 03:47:04 Query Problem from FoxPro???
Previous Message Andreas 2007-06-30 02:14:28 percentages of a column