Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active January 12, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/d53c13c9893ba5a2f67c434b0b4adf1d to your computer and use it in GitHub Desktop.
Save maptastik/d53c13c9893ba5a2f67c434b0b4adf1d to your computer and use it in GitHub Desktop.
A collection of useful SQL queries

Get a list of duplicate records and how many duplicates there of that record

SELECT DISTINCT SOME_FIELD, count(*)
FROM SOME_TABLE
GROUP BY SOME_FIELD
HAVING count(*) > 1

Spatial join based that adds polygon values (b) to intersecting point features (a). Be aware that this does not return a value for those points that are outside a polygon feature

Example question: What council district is each park in?

SELECT a.field1, b.field1
FROM a, b
WHERE st_intersects(st_pointonsurface(a.geom), b.geom)
order by council_dist

Alternatively, we can get the things that don't intersect!

SELECT a.field1
FROM a
left join b
on st_intersects(st_pointonsurface(a.geom), b.geom)
where b.field1 is null

Get counts and acreage of park types

select park_type, count(park_type), round(sum(map_acres)::numeric, 2)
from parks
group by park_type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment