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

  • 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.
    – she_weeds
    Commented Feb 29 at 14:21

1 Answer 1


As explained by she_weeds, it was indeed very simple:

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

Your Answer

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

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