[FWTools] Max() or limit in OGR SQL

Frank Warmerdam warmerdam at pobox.com
Thu Apr 19 23:32:33 EDT 2007


David Fawcett wrote:
> I am using an OGR connection to a shapefile in a MapServer map file in 
> order to use SQL to get the last record in the shape. 
> 
> I have tried to order it by an ID column descending and then use limit 1 
> or rownum = 1, these didn't work. 
> 
> I have also tried WHERE ID = MAX(ID), but I get an error message that 
> indicates that MAX is being interpreted as a string and that the 
> comparison is illegal.
> 
> msOGRFileOpen(): OGR error. ExecuteSQL(SELECT FID, * FROM bryantrip 
> WHERE FID = MAX(FID) ORDER BY timeLoc desc) failed. SQL: Attempt to 
> compare numeric field `FID' to non-numeric value `MAX' is illegal.
> 
> Can anyone suggest OGR SQL to get the last row of the shape? 

David,

I can't think of any way - partly because LIMIT is not implemented in
OGR SQL.  You can order by fid descending though, so you can be sure
you get the last record first if you want.

ogrinfo anno.shp -sql 'select fid, * from anno order by fid desc'

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org



More information about the FWTools mailing list