Arithmetic Division For Two Table In SQL (PostgreSQL)

假如想象 提交于 2021-01-29 20:19:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!