Select to view content in your preferred language

how to get a total rows count in table from query in javascript

4484
5
Jump to solution
10-01-2018 02:37 PM
MRReddy
Regular Contributor

hi

var buildinglayer=http://xxx.yyyyy.com/arcgis/rest/services/Building/PtrsBuilding/FeatureServer/1 

how to find total count of rows in table by query or ("ID" column max number)

i tried with below code, it didn't worked. can anyone suggest a method

var querytask = new Querytask(buildinglayer);
var query = new Query();
query.returnGeometry = false;
query.outFields = ["ID"];

querytask.execute(query, function (result) {
debugger;
console.log(results);
});

Robert Scheitlin, GISP

0 Kudos
1 Solution

Accepted Solutions
TanuHoque
Esri Regular Contributor
0 Kudos
5 Replies
TanuHoque
Esri Regular Contributor

I think you should be able to use QueryTask.outStatistcs to get a count.

here is a sample Query Statistics with SQL Expression | ArcGIS API for JavaScript 3.26 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Malla,

   The QueryTask has more methods then execute. Executeforcount is what you want to use.

https://developers.arcgis.com/javascript/3/jsapi/querytask-amd.html#executeforcount 

Also you need to specify a where clause if you are not providing geometry.

TanuHoque
Esri Regular Contributor

Pls note there is an important distinction between these two options - it is how excuteCount and 'getting count from outStats' are implemented (at least for on-premises/non-hosted map and feature services). Unfortunately can't say that for sure for hosted feature services though.

These implementation results in performance especially when you have a lot of data in your layer (or lots of rows matched by where-clause) - JS API executeCount calls map/feature service query operation with returnCount=True parameter - when that happens, map/feature service asks database to return individual rows (that matches the where-clause) and computes the count by looping through them and returns the count value back to JS API client...

... versus when you execute with outStatistics, JS API calls query operation with outStatistis=[{...}] parameter which results in a query that gets evaluated at the database level - that means map/feature service asks database to do the computation at the database level and return the resultant number instead of return individual rows, that count value is forwarded to JS API client.

Having said that, there is a bit downside to outStatistics approach too - it only works when the data source of the layer is one that supports this kind of query -- which is almost all data sources (such as FileGDB or any enterprise database (e.g. Oracle, SQL Server, PostgreSQL etc.)) except shapefiles. Therefore you should check whether the layer, you are about to query, supports outStatistics.

hope this helps.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Tanu,

JS API executeCount calls map/feature service query operation with returnCount=True parameter - when that happens, map/feature service asks database to return individual rows (that matches the where-clause) and does the count on the client side by looping thru those rows...

Are you sure about this? The docs for executeForCount state the return value is just a number not a featureset.

https://developers.arcgis.com/javascript/3/jsapi/querytask-amd.html#event-execute-for-count-complete 

0 Kudos
TanuHoque
Esri Regular Contributor

Robert,

Sorry about the confusion. Yes, you are right, in the end the result that is returned by map/feature service, contains only a number not a featureset.

When I said 'does the count on the client side by looping thru those rows', I meant the communication between map/feature service (being the client here) and the underlying database.

I will update my previous comment to make that explicit. Thanks for point that out to me 

0 Kudos