3

I have the following table filled out in QGIS. How do I count each fruit present in the 'execucao01' column, obtaining the following result? BANANA = 10 MACA = 13 PERA = 12 LARANJA = 11

enter image description here

enter image description here

1 Answer 1

5

To count the numbers of "BANANA", you can use this expression:

count("execucao01", 
      filter:=(array_contains(
                   string_to_array(
                        substr("execucao01", 2,-1)), 
               '"BANANA"'))= true)
  1. substr("execucao01", 2,-1) will remove leading and tailing braces {} before turning the string into an array by string_to_array() command.
  2. array_contains(+++++, '"BANANA"') determins if the said array contains "BANANA". It then acts as an filter (checks if it returns true) to be included in the count().

Next, to count "MACA", the same expression will be used by replacing "BANANA" by "MACA".

enter image description here

A quick test on your first three rows is seen below.

enter image description here

Repeat the process for other items.

7
  • It worked perfectly. Now I’m going to try to improve it, by searching the list of fruits on another table and making the total.
    – Denilson
    Commented Feb 16, 2021 at 20:48
  • I tried, but I couldn't. I changed my question to put the results in another table with the list of fruits and their totals. It's possible?
    – Denilson
    Commented Feb 17, 2021 at 12:22
  • @Denilson What part of your workflow did not work, may I ask?
    – Kazuhito
    Commented Feb 17, 2021 at 12:51
  • The solution you presented worked very well, but it puts the result in the original table, creating new columns. I tried but I couldn't put the results in a new table where I have 2 fields, 'fruit' name and 'total' (I changed my question to include the example of that table). I tried to use the aggregation function. count (aggregate (layer: = 'TABLE_A', aggregate: = 'concatenate', expression: = "execucao01"), filter: = (array_contains (string_to_array (substr (aggregate (layer: = 'TABLE_A', aggregate: = 'concatenate', expression: = "execucao01"), 2, -1)), '"FRUIT"')) = true )
    – Denilson
    Commented Feb 17, 2021 at 13:13
  • 1
    @Denilson Great. Then, would you open a new question for that part? I will try to answer it asap.
    – Kazuhito
    Commented Feb 17, 2021 at 13:43

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.