In a calculated field in QGIS 3.10.2
I wan't to aggregate values from an array field. For this aggregation I'm using a filter to compare matching id's and return value from another field. I can aggregate values from a text or a numeric field, but I don't know how to reproduce this method with a list of values.
To be more precise:
- myTargetLayer: a reference table which contains id and text values
idTargetLayer (integer) | myField | |
---|---|---|
1 | 1 | Grenache N |
2 | 2 | Cinsaut N |
3 | 3 | Carignan N |
4 | 4 | Viognier B |
5 | 5 | Chenin B |
- mySourceLayer: a layer which contains an array field of id's of target layer and a text field. In this text field I wan't to aggregate text values of the target layer (called 'myField' here). For example if array field is
'1,2,3'
I wan't to have'Grenache N, Cinsault N, Carignan N'
.
id | idTargetLayer (array) | Aggregate Field | |
---|---|---|---|
1 | 9 | 4 | Viognier B |
2 | 8 | 1 | |
3 | 7 | 1 | Grenache N |
4 | 6 | 2, 1 | |
5 | 5 | 2, 1 | |
6 | 4 | 1 | Grenache N |
7 | 5 | 1, 5 | |
8 | 2 | 2, 1 | Cinsaut N |
9 | 19 | 2, 1 | Cinsaut N,Cinsaut N |
10 | 18 | 2, 1 | , |
11 | 17 | 2, 1 | 2,1 |
12 | 16 | 2, 1 |
Simple aggregation
aggregate(
layer:=layer_property('myTargetLayer','name'),
aggregate:='concatenate',
expression:="myField",
filter:= attribute(@parent,'id_source_layer') = attribute($currentfeature,'id_target_layer')
)
If I try to iterate through each value of the array, it returns the same duplicated value.
array_to_string(
array_foreach(attribute($currentfeature,'id_source_layer'),
aggregate(
layer:=layer_property('myTargetLayer','name'),
aggregate:='concatenate',
expression:="myField",
filter:= @element = attribute($currentfeature,'id_target_layer')
)
)
)
How to write an aggregate expression which iterates through each value of an array?
myField
contains?