Postgres - 减去两个查询

时间:2016-05-29 11:05:34

标签: postgresql postgis

我想这样做一个减法:

SELECT 
    (SELECT ST_Y(ST_Centroid(ST_Collect(column1))) AS distance_km 
     FROM table1) - 
    (SELECT ST_Y(ST_Transform(column1,4326)) AS distance_km 
     FROM table1) AS Difference

ST_Y(ST_Centroid(ST_Collect(column1)))只有一行而ST_Y(ST_Transform(column1,4326))有多行。我想按列减去多行,并查看每个多行的结果。通过这个查询我得到:

  

错误:用作表达式

的子查询返回多行

任何帮助?

1 个答案:

答案 0 :(得分:2)

以下查询将针对多行结果集连接单行结果集,并允许对它们进行比较。

SELECT
    a.*,
    b.*,
    a.distance_km - b.distance_kn    AS difference
FROM
(
    SELECT  ST_Y(ST_Centroid(ST_Collect(column1))) AS distance_km FROM table1
)
    a
CROSS JOIN
(
    SELECT ST_Y(ST_Transform(column1,4326)) AS distance_km FROM table1
)
    b