0

In QGIS, I'm working with two layers that are related to each other through the value-relation widget.

Layer 1: TEST

  • fields: "id"; "LIB"; "odc_ID"; "Code"

Layer 2: ASSO_TEST

  • fields: "id"; "LIB"; "CORRESP"

The value relation widget is on ASSO_TEST, field LIB

I'm attempting to generate a list containing one occurrence of each unique value in the LIB field, based on the corresponding values in the Code field within the ASSO_TEST layer.


I encounter an issue when there are multiple features with identical labels, leading to duplicate entries in the value-relation list.

In a previous project, I was able to filter this list using the following expression:

id = array_first(array_agg("id", group_by:= "Label"))

However, in this current project, I'm encountering difficulties and I'm unsure of what I might be doing wrong.

I have tried several approaches, like :

"Lib" = (array_distinct(array_agg("Lib", group_by:='odc_ID'))) 
"Lib" IN (array_agg("Lib", group_by:= 'odc_ID'))
"odc_ID" IN (array_distinct(array_agg("odc_ID", group_by:= "code")))
array_find("odc_ID",array_distinct(array_agg("odc_ID", group_by:= "code")))
"Lib" IN (array_to_string(array_distinct(array_agg("Lib", group_by:= "code")), ', '))
array_contains(array_distinct(array_agg("odc_ID", group_by:= "code")),"odc_ID")

I wouldn't have tried out so many variations of the code if I properly understood what I'm doing. But for my previous projects, I didn't have issues.

Here is a link to download a test project.

Test project

she_weeds
13.8k1 gold badge35 silver badges67 bronze badges
asked Feb 29, 2024 at 13:50
1
  • 1
    You were overthinking it, if you want unique values of LIB then the expression is "id" = array_first(array_agg("id",group_by:="LIB")) - just change the group_by parameter. It's already answered here gis.stackexchange.com/a/431609/98784 and in other places. The filter can be very slow on large datasets. Also, not sure what you meant by filtering LIB based on Code field in ASSO_TEST?... there is no Code field in ASSO_TEST. Commented Feb 29, 2024 at 14:21

1 Answer 1

0

As explained by she_weeds, it was indeed very simple:

"id" = array_first(array_agg("id",group_by:="LIB"))
answered Mar 1, 2024 at 5:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.