Monday, 30 November 2015

ogr - ogrinfo SQL GROUP BY Clause


Does ogrinfo support a GROUP BY clauses? I could not find any good documentation of all the SQL syntax features it supports.


I tried the following:


ogrinfo . -geom=no -sql 'SELECT lsad FROM tl_2011_us_county GROUP BY lsad'

And got the following error:


ERROR 1: SQL: Failed to parse SELECT statement, extra input at BY token.
INFO: Open of `.'
using driver `ESRI Shapefile' successful.


What I really want to do:


SELECT lsad, count(*)
FROM tl_2011_us_county
GROUP BY lsad

What I can do:


SELECT DISTINCT lsad
FROM tl_2011_us_county


And then:


SELECT count(*)
FROM tl_2011_us_county
WHERE lsad = 'foo'

... but that is annoying.



Answer



Depends on the datasource - if OGR needs to fall back to OGR SQL (like when using shapefiles) then no. But if you are using PostGIS for instance you can use GROUP BY.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...