From: | Humberto Carvalho <carvalho(dot)humberto(at)gmail(dot)com> |
---|---|
To: | Sindile Bidla <sindile(dot)bidla(at)gmail(dot)com> |
Cc: | pgsql-novice <pgsql-novice(at)postgresql(dot)org> |
Subject: | Re: How to compute percentages |
Date: | 2012-05-23 11:37:54 |
Message-ID: | CABORsQOGk6mKH25E4OjzSFB0XiSTh0AX9DNhYe7-J4z0=iW_NQ@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Try something like this
select acl1, no_accounts , Round(100*counter/total,2) as percentage
from (select count(1) as no_accounts , cl1
from xpto t
where xxxx) x ,
(select count(1) as total
from abc t
where xxxxx) y;
Best regards,
HC
On 23 May 2012 12:23, Sindile Bidla <sindile(dot)bidla(at)gmail(dot)com> wrote:
> I have a query that is like this:
>
> SELECT cl1, count(cl2) as no_accounts, sum(cl2) as consumption
> FROM tbl1
> WHERE cl2 != 0
> GROUP BY cl1
> ORDER BY cl1;
>
> this gives me the following output
>
> cl1 no_accounts consumption
> a 2 50
> b 1 100
> c 5 30
>
> On the same query i want to calculate percentages no_accounts and
> consumption without having to create a table or view and then computing the
> percentages
>
> cl1 no_accounts percent_no_accounts consumption
> percent_no_consumption
> a 2 25
> 50 27
> b 1 12
> 100 55
> c 5 62
> 30 16
>
>
>
--
Humberto Carvalho
From | Date | Subject | |
---|---|---|---|
Next Message | Oliveiros d'Azevedo Cristina | 2012-05-23 11:40:06 | Re: How to compute percentages |
Previous Message | Sindile Bidla | 2012-05-23 11:23:36 | How to compute percentages |