Обсуждение: percentages of a column

Поиск
Список
Период
Сортировка

percentages of a column

От
Andreas
Дата:
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%




Re: percentages of a column

От
Gregory Stark
Дата:
"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 "%"
frombasket group by fruit_nameorder by "%";
 
fruit_name | count |  %  
------------+-------+-----Bananas    |     5 | 10%Apples     |    15 | 30%Oranges    |    30 | 60%
(3 rows)

--  Gregory Stark EnterpriseDB          http://www.enterprisedb.com