pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
percentages of a column
2+ messages / 2 participants
[nested] [flat]

* percentages of a column
@ 2007-06-30 02:14  Andreas <maps.on@gmx.net>
  0 siblings, 1 reply; 2+ messages in thread

From: Andreas @ 2007-06-30 02:14 UTC (permalink / raw)
  To: pgsql-sql

Hi,

There is a Select that calculates some SUMs of objects.
I'd like to show a list of counts and percentages of this counts based 
on the sum of all counts.
Is that possible in a SELECT statement?

Example:  

Fruit          Count       %
--------------------------
Bananas       5          10%
Apples       15          30%
Oranges     30          60%





^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: percentages of a column
@ 2007-06-30 04:15  Gregory Stark <stark@enterprisedb.com>
  parent: Andreas <maps.on@gmx.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Gregory Stark @ 2007-06-30 04:15 UTC (permalink / raw)
  To: Andreas <maps.on@gmx.net>; +Cc: pgsql-sql

"Andreas" <maps.on@gmx.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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2007-06-30 04:15 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-06-30 02:14 percentages of a column Andreas <maps.on@gmx.net>
2007-06-30 04:15 ` Gregory Stark <stark@enterprisedb.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox