I have two layers with very similar tables
Layer 1:
id | section |
---|---|
1.1 | A |
1.2 | A |
1.3 | A |
2.1 | B |
3.1 | C |
3.2 | C |
At first I needed to summarize all id ́s that belong to one section (e.g.: '1.1, 1.2, 1.3' for section A)
Here is the code:
array_to_string(
array_distinct(
array_sort(
array_agg("id",group_by:="section"))))
Now there is a second layer which unfortunately has a very similar table compared to the first layer. The second layer only contains one more field ("type"). However, in this example it is not possible to join these layers or add the information to the first table which would make things much easier I guess.
Layer 2:
id | section | type |
---|---|---|
1.1 | A | true |
1.2 | A | true |
1.3 | A | false |
2.1 | B | true |
3.1 | C | false |
3.2 | C | true |
My goal is to filter the array (e.g.: '1.1, 1.2, 1.3') depending on the value in the field "type" from the second layer. I only want to display the id ́s of one section for which "type" is true (e.g. '1.1, 1.2' for section A; '2.1' for B; '3.2' for C) Please keep in mind that these are two different layers.
The expression array_agg()
also offers a filter, but I wasn ́t able to perform this referring to another layer. I tried
attribute(get_feature())
as a filter expression but it didn ́t work. Also array_filter()
didn ́t solve my problem.
1 Answer 1
You need to use the aggregate
function. Provided the fields "id", "section" and "type" are string fields and 'layer_2' is the name of the second layer:
array_to_string(
array_distinct(
aggregate('layer_2', 'array_agg', "id", attribute(@parent,'section')="section" AND "type"='true', order_by:="id")
),
', '
)
Using such expression to create a string virtual field named "aggregated" in layer_1, the resulting table will be:
id | section | aggregated |
---|---|---|
1.1 | A | 1.1, 1.2 |
1.2 | A | 1.1, 1.2 |
1.3 | A | 1.1, 1.2 |
2.1 | B | 2.1 |
3.1 | C | 3.2 |
3.2 | C | 3.2 |
Explore related questions
See similar questions with these tags.
order_by
doesn´t work. I think it needs to begroup_by
but this expression is not possible withaggregate
.