# How to compute percent of a field

I have a layer with a column conaining population values. I want to write an expression to compute in a new field the percentage of population each polygon has in relation to the total population.

I know I can use basic stats to get the SUM of area column, but I want an automatic process to repeat over several fields).

• Thanks for the answers. I know I can export the data, do the calculations in Excel/LibreOffice and bring it back to QGis. However, I'm exploring a way to do everything inside QGis to create a model with Sextante. Commented May 24, 2014 at 13:04

It will depend on how your data is setup. But if, for example, you have 5 records in a table, with each record representing a different location with a different population, you could do one of two things:

``````1) Have a column representing the populations (A = state population; B = country population)
2) Enter the constant total population
``````

Option 1 is better for larger datasets and datasets that do not have a constant total population. Either way, you will want to do the following:

``````1) (A / B) * 100
2) (A / constant value) * 100
``````
• Thanks for your answer. The problem is how to (automatically) compute "B" (or get the value of total population to reuse on a new formula). To give an idea, I have 8 datasets with 12 population columns each so I need to produce almost 100 new columns representing percentages. Commented May 24, 2014 at 13:03

SELECT population, 100.0 * population/sum(population) OVER () FROM yourtable;

• It is unfortunate that denormalization is so favorable.
– ike
Commented May 22, 2014 at 13:11