1

I am trying to build a graphical model. This model takes a vector layer as input. I have the first field as a grouping variable and all remaining fields are numeric. I would like to total each column based on the grouping variable in first field. In some cases, I have first two fields as grouping variable and the rest are numeric.

In 'statistics by category tool', i am able to use only one grouping variable and one field in 'Field to calculate statistics on'. Is it possible to add multiple numeric field to 'Field to calculate statistics on' and multiple categorial(string) variables to 'Field with categories'? Maybe just a syntax issue? I tried with single qoutes and double quoutes around field names and with a comma separating the two fields . But I get error.

Is there any tool within QGIS to acheive this? I am not looking for 'plugin' solutions.

7
  • Have you tried the sum() expression?
    – Erik
    Commented Jun 21 at 11:22
  • Sorry, your question is not clear to me. What is your problem? You have one or two fields for grouping in the attribute table? And what exactly does not work in statistics by category tool? Please use the right terminology of the tool, otherwise we don't understand you: in the tool, you have Field to calculate statistics on and Field(s) with categories: so what are you speaking about? Please edit your initial question, otherwise it risks to be closed.
    – Babel
    Commented Jun 21 at 11:42
  • I edited the question. @Erik, sum works for one field. I wish to summarize multiple fields. In fact I wish to total all columns except the grouping variable.
    – BKS
    Commented Jun 21 at 12:20
  • So you could run sum() for each column?
    – Erik
    Commented Jun 21 at 13:31
  • 3
    It is possible to create a virtual layer using SQL, like: SELECT groupfield1, groupfield2, sum(col1), sum(col2), sum(col3) FROM mylayer GROUP BY groupfield1, groupfield2
    – Bera
    Commented Jun 21 at 13:44

1 Answer 1

2

You can run Statistics by categories in Batch mode. To calculate the statistics on different fields, add a row for each field and fill in this field in Field to calculate statistics on. For Field(s) with categories, select one or more fields for grouping:

enter image description here

You get a separate output for each Field to calculate statistics on you defined:

enter image description here

2
  • Thank you. I cannot use batch mode because I do not know how many fields I will have in my data input in graphical modeler. I only know that my first field will be categorial grouping variable and all other columns (any number of columns) will be numeric. May be this did not come across clearly in my main question. And in a small chance I would have 2 grouping variables and all other columns (any number of columns) will be numeric. I tried executesql algorithm but also there I could not find a way how to total all columns in the table except the first column with grouping variable.
    – BKS
    Commented Jun 21 at 14:31
  • You can automatically create an array of all fieldnames with map_akeys(attributes()), then you can filter out the one or two fieldnames containing the group variables. Other than batch mode, I don't see any other possibility except using code.
    – Babel
    Commented Jun 21 at 14:41

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.