我想在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(*)
。