avinash Thakur
avinash Thakur

Reputation: 13

how to calculate percentage in sql without using traditonal methods

         A        B       A/B   
       461264   307638  66.7178 
       334673   217099  64.869  
       372045   220354  59.2278 
       427186   181755  42.547  
       435871   214099  49.1198

Total 2031039   1140945 56.1754

-> this average is calculated by using (214099/435871) * 100.

I want a solution in which I have to take only one column i.e a/b and by using this only I want to calculate the average percentage.

Upvotes: 0

Views: 57

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269743

Are you looking for this?

select avg(a / b)
from t;

Or this:

select avg(a) / avg(b)
from t;

The two answers are different. I can't easily tell from your question which version you want.

Upvotes: 1

JohnHC
JohnHC

Reputation: 11195

For the average percentage (not overall percentage), perform the calc, then select the average:

select avg(x.pc)
from
(
select a/b as pc
from MyTable
) x

For the overall percentage, you need:

select x.a/x.b
from
(
select sum(a) a, sum(b) b
from MyTable
) x

Upvotes: 1

Related Questions