I've got a colleague who wants to create a (concatenated) label based on a 1:n relation between some shapes and related tables. They asked me if this was possible in QGIS - and I think it should.

I found this: QGIS: relations in label expressions

However, I failed to generalise this for 1:n and m:n relations. The solution provided by @ben-w isn't using the relation, and the solution by @alexander-novikov uses relation_aggregate() which is not really suitable for simply just getting multiple character values our of a relation.

Can someone point me to to right direction, and maybe provide some syntax explanation?

(NB: Sorry not to provide a reproducible example. I not using QGIS expressions normally, I am coming from R using {dplyr}, and I don't speak SQL, nor Python. Different syntax, maybe sometimes different logic. R is no solution for my colleague, so I try to learn something new.)

| improve this question | | | | |

You can use the aggregate function in the expression editor:

 aggregate( layer:= 'N_LAYER',
            concatenator:=', ',
            filter:=attribute(  $currentfeature ,'FOREIGNKEY_IN_N_LAYER')=attribute(  @parent ,'KEY_IN_1_LAYER'))

where you tell the aggregate function in the filter criteria the relation between the key/foreign key. Be aware that $currentfeature belongs to the n_layer and @parent to the 1_layer in your 1:n relation.

| improve this answer | | | | |
  • Thanks. Took me a while to parse this. expression := gets whatever field (or even concat("attribute_field1", ', ' "attibute_field2"), concatenator := sets the separator for the 1:n relations, i.e. multiple matches. The filter := part is what I was looking for. I think I could not have learned this from docs.qgis.org/testing/en/docs/user_manual/working_with_vector/… (and I'm not yet sure how this, um, relates to relation_aggregate. I'm going try to make this work for n:1 and m:n relations as well. Your help is much appreciated. :) – George William Russel's pen Apr 29 '19 at 12:35

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.