QGIS – How to Sum Values from Multiple Features with a Given Attribute

qgis

I'm working with two layers right now – a line layer called Water Lines and a polygon layer called Land Areas. The Land Areas layer has polygons in which water runoff occurs during precipitation events. That Runoff Estimate from each polygon feature is received by a Receiving Water Line Feature, which corresponds to a feature of the same name in the Water Lines layer. A single Water Line feature can receive runoff from multiple Land Areas polygon features.

I want the field Total Collected Runoff for a given Receiving Water Line Feature Name in the Water Lines layer to sum the Runoff Estimate values for all polygons in the Land Areas layer with the matching Water Line Feature Name in the Receiving Water Line Feature field.

Water Lines attribute table

Land Area attribute table

Best Answer

You can use an expression like this to update the field Total Collected Runoff (gal) of the layer Water Lines.

I took the field names currently displayed, but maybe there are aliases, so modify it in the expression if needed.

aggregate(
    layer:= 'Land Areas',
    aggregate:= 'sum',
    expression:= "Runoff Estimate (gallons)",
    filter:= "Receiving Water Line Feature" = attribute(@parent, 'Water Line Feature Name')
)
Related Question