如何在postgresql中对来自两个不同表的数据进行计算



我想在postgresql中计算一些特性的比率,但使用来自两个不同表的数据。

table 1我想查询所有行的计数:

select count(*) as total from table1

,然后使用上面的total计算执行计算,如下所示:

select count(class) / total as ratio
from table2

total来自表1count(class)来自表2

我怎么能做到这一点,因为没有公共字段来连接它们?

使用子查询:

select
(select count(class) from table2) /
(select count(*) as total from table1) as ratio

count(class)可以是count(distinct class),count(class)也可以是count(*)

最新更新