4

Using QGIS 3.6.1 I have a layer with areas and a table with entries related to the areas:

enter image description here

A relation is defined like this:

enter image description here

What I would like to achieve is to label the areas with the number of related table entries using relation_aggregate function as shown here:

enter image description here

relation_aggregate ('my_relation', 'count', 'text') || ' Table entries'

As can be seen in the screenshot (Output preview), the label expression delivers a valid result as expected (e.g. "4 Table Entries"), but labels are not shown in the map:

enter image description here

Another thing I tried using the aggregate function is the following:

enter image description here

with_variable('my_id', "id", aggregate('table', 'count', 'id', "flaeche_id" = @my_id)) || ' Table Entries'

Which gives a totally weird result depending on which area is shown in the current map extent, i.e.

1) Both areas labelled with "4 Table entries", but the lower has only one related table entry (see table above):

enter image description here

2) If the map extents only to the southernmost area, the label shows correctly "1 Table entries":

enter image description here

So all in all with my first approach I have no labels and with the second approach I have wrong labels.

I think I'm little stuck with the aggregate... functions in QGIS 3, am I using them the wrong way or are there any other approaches to the labeling I want to achieve?

2
  • did you try eval(relation_aggregate ('my_relation', 'count', 'text') || ' Table entries')?
    – PieterB
    Apr 4, 2019 at 10:09
  • just did so, same result either. no labels. Apr 4, 2019 at 10:19

1 Answer 1

2

you have to double quote (") your last argument:

relation_aggregate('my_relation', 'count', "text") || ' Table entries'

the description of relation_aggregate says:

relation_aggregate(relation,aggregate,expression[,concatenator])

[ ] marks optional components

So if you just want to use a fieldname as expresion, you have to refer to it as "my_attribute", which is also standard in other expressions.

Somehow, the fieldcalulator returns a result when using a single quote ('), bizar ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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