query related records attributes

1014
2
Jump to solution
08-27-2013 11:55 AM
KaitlynnDavis
Occasional Contributor
Is it possible to extend a query to the attributes of a related table?

I have a bunch of drop down menus designed to let users filter out my map layer

I would like to execute a query that on completion highlights features that satisfy a particular 'where' clause AND whose related record also satisfies a 'where' clause

Joining is the easiest thing to do, but I can't join because of a bug that affects the naming conventions of attributes in published map services. So I'm left with relates
0 Kudos
1 Solution

Accepted Solutions
JasonZou
Occasional Contributor III
TO my understanding, there is no single WHERE clause you can apply to accomplish what you want. You might end up with two queries: first to filter the related table based on its where clause; once returned, use its result to query the feature layer based on the related record ids and the where clause for the feature layer. Then you can process the result the way you like. Don't try to query the feature layer first; otherwise, you will end up with more queries.
require([   "esri/tasks/Query", "esri/tasks/QueryTask", "esri/tasks/RelationshipQuery"  ], function(Query, QueryTask, RelationshipQuery) {   var queryTask = new QueryTask("related data url");    var query = new Query();   query.where = "where clause to filter the related data";     queryTask.executeForIds(query,function(relDataIds){      var relQuery = new RelationshipQuery();      relQuery.definitionExpression = "where clause to filter the feature";      relQuery.objectIds = relDataIds;      relQuery.relationshipId = 3;      relQuery.returnGeometry = true;      queryTask.executeRelationshipQuery(relQuery, function(featureSet) {          // do whatever you like to process the final result      });   }); });

View solution in original post

0 Kudos
2 Replies
JasonZou
Occasional Contributor III
TO my understanding, there is no single WHERE clause you can apply to accomplish what you want. You might end up with two queries: first to filter the related table based on its where clause; once returned, use its result to query the feature layer based on the related record ids and the where clause for the feature layer. Then you can process the result the way you like. Don't try to query the feature layer first; otherwise, you will end up with more queries.
require([   "esri/tasks/Query", "esri/tasks/QueryTask", "esri/tasks/RelationshipQuery"  ], function(Query, QueryTask, RelationshipQuery) {   var queryTask = new QueryTask("related data url");    var query = new Query();   query.where = "where clause to filter the related data";     queryTask.executeForIds(query,function(relDataIds){      var relQuery = new RelationshipQuery();      relQuery.definitionExpression = "where clause to filter the feature";      relQuery.objectIds = relDataIds;      relQuery.relationshipId = 3;      relQuery.returnGeometry = true;      queryTask.executeRelationshipQuery(relQuery, function(featureSet) {          // do whatever you like to process the final result      });   }); });
0 Kudos
KaitlynnDavis
Occasional Contributor
Thank you, this approach is working for me. In my case where there are two layers being queried, I must first do a relationship query against layer 1 to retrieve the related records that meet my 'where' criteria, and then with the results do a second relationship query against layer 2. The results of the second relationship query are assigned graphic properties and added to the map
0 Kudos