## Re: How to compute percentages

From: "Oliveiros d'Azevedo Cristina" "Sindile Bidla" ,"pgsql-novice" Re: How to compute percentages 2012-05-23 11:40:06 36695DD8501F4BF59C7BD38140FAF78E@marktestcr.marktest.pt (view raw, whole thread or download thread mbox) 2012-05-23 11:23:36 from Sindile Bidla  2012-05-23 11:37:54 from Humberto Carvalho  2012-05-23 11:40:06 from "Oliveiros d'Azevedo Cristina" pgsql-novice
```Hi,
Sindile,

Best,
Oliver

SELECT cl1, count(cl2) as no_accounts,
count(cl2) * 100.0 / (SELECT count(cl2) FROM tbl1) as percent_no_accounts,
sum(cl2) as consumption,
sum(cl2) * 100.0 / (SELECT sum(cl2) FROM tbl1) as percent_no_consumption
FROM tbl1
WHERE cl2 != 0
GROUP BY cl1
ORDER BY cl1;
----- Original Message -----
From: Sindile Bidla
To: pgsql-novice
Sent: Wednesday, May 23, 2012 12:23 PM
Subject: [NOVICE] How to compute percentages

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

```

### pgsql-novice by date

 Next: From: Bartosz Dmytrak Date: 2012-05-23 11:50:59 Subject: Re: Page Usage Information Previous: From: Humberto Carvalho Date: 2012-05-23 11:37:54 Subject: Re: How to compute percentages