Skip to content
Advertisement

Nested self join and creating multiple sums

I have a basic parent / child scheme for expenditures:

enter image description here

The underling data is the same so I just added a category column and parent_id. These have child records:

enter image description here

I am trying to aggregate the totals form the orders, related orders and difference between the two like this:

enter image description here

Which is grouped by the orders overall then I am also looking for something like this:

enter image description here

I can get the order_amount no problem either way. That’s a simple JOIN and SUM.

I am stuck on the secondary JOINS given that I have to JOIN the invoices expenditures to the orders then JOIN the invoice expenditure items and SUM that up.

I am looking for direction on the correct JOIN or if there is a better way to approach this with some sort of subquery etc.

Advertisement

Answer

To sump up by order, one solution would be to use a conditional aggregate query. A trick is to check the category to decide whether to use the value from column expenditures.id or from column expenditures.parent_id as grouping criteria:

Demo on DB Fiddle:


The second query, that sums up by item code, basically follows the same logic, but groups by idem code instead:

Demo:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement