I'm working with a spatial PostgreSQL database that contains species records, there is a point layer 'spp_points'
, each point has a "spp_ID"
and "spp_score"
field. I also have a polygon layer 'field_poly'
.
I need to work out the sum of unique "spp_id"
scores only within each polygon, so multiple records of the same species should only be counted once.
I have tried using the following query but this calculates the total of all species, so multiple instances of the same species are included in the sum:
SELECT site.gid AS field,
SUM(spp.total) AS field_score,
site.geom
FROM (SELECT DISTINCT spp_id,
spp_score AS total,
geom
FROM spp_points) AS spp
JOIN field_poly AS site ON ST_Within(spp.geom, site.geom)
GROUP BY site.gid
I'm not sure where I need to go from here.
"spp_ID"
possess a different"spp_score"
? Or if there are several points with the same"spp_ID"
they will have the same"spp_score"
?"spp_score"
is a value from 1-10, a given"spp_ID"
will have the same"spp_score"
so for instancespp_A
will always have a value of8
. Note that species with different"spp_ID"
may have same"spp_score"
though."site.gid"