Mik
Mik

Reputation: 17

Oracle. How to calculate % in one select

Good afternoon, can you help me?

I have a table of "routes" Me need calculate count all routes, and count routes with "where". And % two from one.

For example: select count(routes) from table1 "~ 150000 items"

select count (routes) from table1 where distance < 100 "~ 15000 items"

How to get a number 10%?

Upvotes: 0

Views: 43

Answers (2)

mjpolak
mjpolak

Reputation: 769

Try this:

select count(CASE WHEN distance < 100 THEN 1 ELSE 0 END)/count(routes)*100 from table1 where distance < 100

Upvotes: 0

Littlefoot
Littlefoot

Reputation: 142958

You could conditionally aggregate values:

select sum(case when distance < 100 then 1 else 0 end) /
       count(*) * 100 as result
from table1;

Or (worse), use current queries as subqueries:

select
  (select count(routes) from table1 where distance < 100) /
  (select count(routes) from table1) * 100 as result
from dual;

Upvotes: 1

Related Questions