问题
I am trying to do a very simple division in SQL (PostgreSQL). I want to get the count of all the record from two tables, and divide them and output to another table.
For example, we have two tables, table1 and table2
SELECT COUNT(*)
FROM table1;
SELECT COUNT(*)
FROM table2;
Now the above queries will be both one column one row, which is the count of the table1 and table2.
I would like to get the two number divide (COUNT(TABLE1)/COUNT(TABLE2)), how should I do this in SQL?
回答1:
Well, you can use subqueries:
SELECT (SELECT COUNT(*) FROM table1) / (SELECT COUNT(*) FROM table2);
This does integer division. If you want a real number:
SELECT (SELECT COUNT(*) FROM table1)::numeric / (SELECT COUNT(*) FROM table2);
来源:https://stackoverflow.com/questions/53735397/arithmetic-division-for-two-table-in-sql-postgresql