[GIS] Querying through Relations

qgisrelates

Right now I am stumped on how to get two layers to interact. I have one layer that is just a map with town borders, with attributes for town codes. My other layer are data for businesses, wages for example. I setup a relate in the project properties menu between them using town codes, and I can see in the attribute table the the map layer knows what businesses exist in each town now.

However, I can't seem to make use of it. If, for example want to select by expression to just select towns where the average wage is over a certain level, I can't even begin because I can't write an expression using values from both layers, even though I set them up in a relation.
How would I really go about doing this?

I am trying to figure out how to query, select, or anything using attributes from two related layers.

Best Answer

Instead of a relate, have you tried joining the tables?

Assuming QGIS handles relates and joins the same as ArcMap, you will not be able to query on the relate. Basically, a relate just defines a relationship within the map document, and does nothing to relate the tables in the geodatabase (such as a join). In order to query, a database relationship needs to be defined.

On a quick search for QGIS relates vs joins, I didn't see anything that really spelled out the difference, so I'm using a link to an ArcMap help page that defines relates vs joins. I know it's not QGIS, but I assume that they are defined similarly. Quoted text below in case of future link failure.

ArcMap provides two methods to associate data stored in tables with geographic features: joins and relates. When you join two tables, you append the attributes from one onto the other based on a field common to both. Relating tables defines a relationship between two tables—also based on a common field—but doesn't append the attributes of one to the other; instead, you can access the related data when necessary.

Typically, you'll join a table of data to a layer based on the value of a field that can be found in both tables. The name of the field does not have to be the same, but the data type has to be the same; you join numbers to numbers, strings to strings, and so on. You can perform a join with either the Join Data dialog box accessed by right-clicking a layer in ArcMap or the Add Join geoprocessing tool.

Suppose you obtain daily weather forecasts by county and generate weather maps based on this information. As long as the weather data is stored in a table in your database and shares a common field with your layer, you can join it to your geographic features and use any of the additional fields to symbolize, label, query, or analyze the layer's features.

Unlike joining tables, relating tables simply defines a relationship between two tables. The associated data isn't appended to the layer's attribute table like it is with a join. Instead, you can access the related data when you work with the layer's attributes.

...

Relates defined in ArcMap are essentially the same as simple relationship classes defined in a geodatabase, except that they are saved with the map instead of in a geodatabase.

Related Question