I have a pretty specific question and I dont know how to approach this.

I have a polygon layer for subbasins in the Danube area (HydroShed) which also contains information for water discharge.

The first picture shows the attribute table. Each subbasin has a "Hybas_ID" which contains information for the Pfafstetter Code. Most of these subbasins have information for downstream neigbours, which is portrayed in the "Next_Down" (showing the Hybas_ID of the specific subbasin it flows into) column.

Some of these subbasins also have more than one upstream neighbour, so for example subbasin "2080452200" and "2080452210" flow into the subbasin "2080453270" (Picture 1).Attributetable Subbasins My question is: Can I combine/summarize the water discharge information (column "TD") from the first two subbasin and attach it to the subbasin these two flow into (into a new column i.e.)? This has to be done for every subbasin which has upstream neigbours, is there a possibility/tool or function to do this in one go? There are over 1300 subbasins, so to do it by hand wouldn't really be an option. Unfortunately I'm not really frequent in SQL.

  • Does the targeted 2080453270 subbasin itself appear in the "HYBAS_ID" attribute field, and be a part of this calculation? (In other words, your expected outcome (total sum of "TD") is {TD of 2080452200 + TD of 2080452210} plus {TD of 2080453270} ?)
    – Kazuhito
    Jul 5, 2020 at 7:36
  • @BERA I'd love to, but I don't know how. I'm still fairly new here.. how can I share a .zip folder?
    – Alwin
    Jul 6, 2020 at 7:50
  • 1
    @Kazuhito It does appear in the "HYBAS_ID" attribute field, as long as it is inside the bordes of my study area. I need the upstream flow data for another model, so only the two inflow "TD" would be relevant in this case. So {TD of 2080452200 + TD of 2080452210} would be the calculation for this example.
    – Alwin
    Jul 6, 2020 at 7:54

1 Answer 1


Using a sample data (below image):

enter image description here

Start the Field Calculator and input an aggregating expression: sum("TD", group_by:= "NEXT_DOWN")

enter image description here

Then all the rows which share the same NEXT_DOWN will be given the sum of upstream inflows.

enter image description here


If you need to associate the UPSTREAMS value to the HYBAS_ID (rather than NEXT_DOWN), you could use a look-up expression:

attributes(get_feature('your_layer_name', 'NEXT_DOWN', "HYBAS_ID"))['UPSTREAMS']
  1. get_feature('your_layer_name', 'NEXT_DOWN', "HYBAS_ID") part will find the first record of which NEXT_DOWN is equal to the current feature's HYBAS_ID.
  2. Then attributes(...)['UPSTREAMS'] part will extract UPSTREAMS value from the record looked-upped by the above get_feature() part.

NB. If the get_feature() part cannot find any matching record, it will simply returns its own UPSTREAMS value... which is probably confusing. Hopefully your dataset has full set of HYBAS_ID-NEXT_DOWN pairs.

  • 1
    Thanks a lot, that was very helpful! But I have one more question: I need the "UPSTREAMS" results to appear at the "HYBAS_ID" which matches the "NEXT_DOWN" ID. So that, like in your example, the results for the sum of "NEXT_DOWN": "2080453270" match the "HYBAS_ID" of "2080453270". (something like this 'if "HYBAS_ID" = "NEXT_DOWN";Qup). I hope thats understandable. The "HYBAS_ID" is a fixed ID for every feature, so the "UPSTREAMS" result should only appear once for every specific "HYBAS_ID".
    – Alwin
    Jul 6, 2020 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.