Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple sql query, combine results and divide

Tags:

sql

mysql

I'm trying to get 2 counts from 2 tables and work out the percentage like for a MySQL db:

  1. select field_one, count(*) as COUNT_ONE from table1 group by field_one;

  2. select other_field,count(*) as COUNT_TWO from table2 group by other_field;

I want to combine the results and have FINAL_COUNT=(COUNT_ONE/COUNT_TWO) * 100 for percentage ?

like image 842
piet Avatar asked Aug 19 '10 12:08

piet


2 Answers

quick and dirty:

select (a.count_one / b.count_two) * 100 as final_count from 
(select field_one, count(*) as count_one from table1 group by field_one) a,
(select field_two, count(*) as count_two from table2 group by field_two) b
where a.field_one = b.field_two
like image 185
Fosco Avatar answered Oct 21 '22 03:10

Fosco


select sum(((QUERY FROM TABLE 1) / (QUERY FROM TABLE 2)) * 100) as percentage
like image 24
gabe3886 Avatar answered Oct 21 '22 01:10

gabe3886