For a web application I am working on, I currently have a feature layer with ID 0 (Street Lights) and feature layer with ID 1 (Maintenance Records Table). Multiple records in layer ID 1 are related to ONLY 1 record in layer ID 0 (Street Light have 0 or many maintenance records). I am attempting to perform a query on layer ID 0 (such as, select all of the traffic lights that have a wattage over 400). I then want to perform a query on layer ID 1 and select only the records that are associated with the ones from the first query.

Therefore, I want all of the maintenance records for lights that have a wattage over 400. I understand how to do this with a Relationship Query. However, the final result is that I then want to perform another query on ONLY those selected records based on the date of maintenance.

My problem is that a Query Task only seems to work on a feature layer URL. Is there anyway that anyone knows how to perform a query on another query's results instead of a feature layer URL? Thanks.

UPDATE: I guess another way to ask this is, If I run the selectFeature function on a feature layer, can I run another selectFeature function on only those previously selected features?


Let's say that layer 0 has a field called 'id', and layer 1 can be joined to layer 0's id field by the field called 'lightId'. The first step to making the selection is to collect all the selected layer 0's id field values from your first query into an array, like so:

// previously loaded "dojo/_base/array" library into the array object
// query result from the first query in variable "featureSet"
var idList = array.map(featureSet.features, function (feature) {
    return feature.attributes.['id'];

Next, construct the first half of the where clause for the second query using that idList. You'll use the TSQL 'in' operator to test if the lightId value is in the list you will format.

var where = "lightId IN (" + idList.join(",") + ") AND ";
// comment out the item above and uncomment the line below if the lightId is of type string
// var where = "lightId in ('" + idList.join("','") + "') AND ";

Notice the trailing and at the end. Now you can append any other search criteria to that where clause.

where += "COST_TO_REPLACE < 1000000"; // add to the where clause as you see fit.
// set up the second query parameters
query2 = new Query();
query2.where = where;

// after you set the rest of the parameters, execute the query
queryTask2.execute(query2, tableQueryResponse, tableQueryError);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.