AnsweredAssumed Answered

How to query only top 10 records?

Question asked by bflessner on May 21, 2015
Latest reply on May 21, 2015 by csgeosol1

Is there a way to query a feature layer to return the highest ten values for a field with the JavaScript API? I’m developing an app and I’d like to just have the server return the needed records (for better performance), instead of returning several thousand and just using the top ten. Is this possible?


I've tried using the query.num property but that only works for hosted services. I've also looked into enabling pagination on the map service so that I can specify a query.resultRecordCount property, but it looks like that won’t be possible ( with a file geodatabase back end. Also no support for a SELECT TOP … SQL query on file geodatabases. I could limit the "Maximum Number of Records Returned by Server" on the service properties, but this would require a whole new service because I need to return all records for display on the map (Not an elegant solution).

var topTenQuery = new Query();
topTenQuery.returnGeometry = false;
//topTenQuery.where = "ACCRATE > 0 limit 10"; // causes error
//topTenQuery.where = "ACCRATE IN (SELECT TOP 10 ACCRATE FROM [featureLayer])"; // causes error
topTenQuery.outFields = ['OBJECTID', 'RDNAME', 'ACCRATE'];
topTenQuery.orderByFields = ['ACCRATE DESC'];
//topTenQuery.resultRecordCount = 10; //causes error because pagination is not enabled
featureLayer.queryFeatures(topTenQuery, resultsHandler);

Thanks for any help!