4

I have many columns, about 30 and each of those has some attributes. I want to merge those columns into one and collect all attributes in a row of columns. I tried with this post Combining Columns QGIS Attribute Table but it is not for what I am looking for.

I have an example in MS Excel that I want that without commas and spaces. Attributes have to be separated with one comma.

enter image description here

With this script, if some fields are empty, I have got NULL fields.

enter image description here

5
  • 1
    Have you had a look at the expressions coalesce and try?
    – Erik
    Dec 2, 2019 at 12:09
  • I used coalesce, but only first attribute was copied
    – nagib
    Dec 2, 2019 at 12:14
  • Please provide the expression you used.
    – Erik
    Dec 2, 2019 at 12:15
  • 1
    coalesce( "spec" , "spec_1" , "spec_12" , "spec_1_13" , "spec_1_14" , "spec_1_15" , "spec_1_16" , "spec_1_17" , "spec_1_18" , "spec_1_19" , "spec_1_20" , "spec_1_21" , "spec_1_22" , "spec_1_23" , "spec_1_24" , "spec_1_25" , "spec_1_26" , "spec_1_27" , "spec_1_28" , "spec_1_29" , "spec_1_30" , "spec_1_31" , "spec_1_32" , "spec_1_33" , '0')
    – nagib
    Dec 2, 2019 at 12:42
  • Yeah, and have you read how the expression works?
    – Erik
    Dec 2, 2019 at 12:43

1 Answer 1

5

This expression does the trick

array_filter(
    map_avals(
        attributes()
        ),
    @element <> ''
    )
  1. attributes() : get all fields with their corresponding values

  2. map_avals() : gives you just the values as an array

  3. array_filter(array, expression) : the expression @element <> '' filters out the empty values

2
  • With this : array_filter( map_avals( attributes( )), @element <>''), I got empty fields. I forgot to say, some columns are in between that I have to exclude from those columns. I have to pick columns manually, (by hand). Something like the second picture
    – nagib
    Dec 2, 2019 at 12:02
  • Nice expression! array_to_string( array_filter ... ) converts the array to a string. It works in QGIS 3.10, latest QGIS LTR 3.4.13 doesn't have the attributes() function. Dec 3, 2019 at 1:04

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.