0

I'm using PostgreSQL 10 and trying to run this query. I started with a CTE which I am referencing as 'query.'

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

The query runs but the problem lies in the 'markeshare' column. It returns the same answer with or without the SUM operator and returns one, which appears to make both the attract_score and the tot_attract_score the same. Why is the SUM operator read the same as the expression above it?

1
  • Probably because you're grouping by amenity_size. Commented Feb 22, 2018 at 19:54

1 Answer 1

1

This is occurring specifically because each combination of columns in the group by clause uniquely identifies one row in the table. I don't know if this is intentional, but more normally, one would expect something like this:

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;

This is not your intention, but it does give a flavor of what's expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.