我正在使用PostgreSQL 10并尝试运行此查询。我开始使用CTE,我将其称为“查询”。
SELECT
ROW_NUMBER()OVER() AS my_new_id,
query.geom AS geom,
query.pop AS pop,
query.name,
query.distance AS dist,
query.amenity_size,
((amenity_size)/(distance)^2) AS attract_score,
SUM((amenity_size)/(distance)^2) AS tot_attract_score,
((amenity_size)/(distance)^2) / SUM((amenity_size)/(distance)^2) as marketshare
INTO table_mktshare
FROM query
WHERE
distance > 0
GROUP BY
query.name,
query.amenity_size,
query.geom,
query.pop,
query.distance
查询运行但问题出在“markeshare”列中。它返回相同的答案,有或没有SUM运算符,并返回一个,这似乎使attract_score和tot_attract_score相同。为什么SUM运算符与上面的表达式读取相同?
答案 0 :(得分:1)
这种情况特别发生,因为group by
子句中的每个列组合都唯一地标识了表中的一行。我不知道这是否是故意的,但通常情况下,人们会期待这样的事情:
SELECT ROW_NUMBER() OVER() AS my_new_id,
query.geom AS geom, query.pop AS pop, query.name,
SUM((amenity_size)/(distance)^2) AS tot_attract_score,
INTO table_mktshare
FROM query
WHERE distance > 0
GROUP BY query.name, query.geom, query.pop;
这不是你的意图,但确实能说明预期的结果。