Calculating sum of children's balance for each parent

Hatik

I have two tables having parent-child relationship

Table1
| Account_no | Main_account_no | bla bla
+------------+-----------------+----------
|     234    |        111      |  
|     235    |        111      |  
|     222    |        112      |  
|     220    |        112      |  

The second:

Table2
| Account_no | Balance         | bla bla
+------------+-----------------+----------
|     234    |        10       |  
|     235    |        15       |  
|     222    |        55       |  
|     220    |        45       |  

What I need is to calculate the sum of balance of child accounts for each parent account. The expected result is

| Main_account_no | Balance |
+-----------------+---------+
| 111             |  35     |
| 112             |  100    |

I am trying to use

SELECT MAIN_ACCOUNT_NO, SUM((SELECT BALANCE FROM TABLE2 WHERE ACCOUNT_NO = A.ACCOUNT_NO)) OVER (PARTITION BY MAIN_ACCOUNT_NO)
FROM TABLE1 A
GROUP BY MAIN_ACCOUNT_NO

But I get the ORA-00979: not a GROUP BY expression

Where did I do wrong?

Aidan Kane

Since this apparently isn't homework... :-)

First let's get the main_account_nos alongside the balances:

select
    Table1.Main_account_no,
    Table1.Account_no,
    Table2.Balance
from
    Table1
    join Table2 on Table1.Account_no = Table2.Account_no

Gives

| Main_account_no | Account_no | Balance         |
+-----------------+------------+-----------------+
|        111      |     234    |        10       |
|        111      |     235    |        15       |
|        112      |     222    |        55       |
|        112      |     220    |        45       |

Now we can easily group as required:

select
    Table1.Main_account_no,
    sum(Table2.Balance) as total
from
    Table1
    join Table2 on Table1.Account_no = Table2.Account_no
group by
    Table1.Main_account_no

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calculating the sum of a variable within all children (recursive)

From Dev

Parent div takes height of first child element and not the sum of all children's height

From Dev

Parent div takes height of first child element and not the sum of all children's height

From Dev

SUM operation on attributes of children of multiple parent records

From Dev

SUM operation on attributes of children of multiple parent records

From Dev

d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

From Dev

jQuery loop through multidimensional array and display children's for each parent array

From Dev

Communicate a change in one of children to parent's parent

From Dev

PHP How to total up the sum from each balance in a foreach loop

From Dev

PHP how to move the children up to each parent

From Dev

Match heights of children within each parent

From Dev

Calculating scene's children's matrixes without rendering

From Dev

Calculating a Sum

From Dev

Calculating a Sum

From Dev

Rails: List all of Parent's children in Parent's Show

From Dev

Parent div not expanding to children's height

From Dev

Laravel parent/children relationship on it's own model

From Dev

Parent container's height is not stretching for children?

From Dev

Parent height not covering children's padding

From Dev

sum of cases in grand-parent, parents, children,, and etc

From Dev

Parent value as sum of all children values within nested javascript object

From Dev

Sum content values for each parent node

From Dev

Get count of children from each parent.SQL Server 2012

From Dev

XML file parsing - Get data from each parent and their own children

From Dev

Get each Children element that has a class and insert before its parent

From Dev

Use recursion to get parent value and children value and all its children's children value

From Dev

parent div's width larger than children's width in Firefox

From Dev

Calculating customer balance from QuickBooks data (QBXML)

From Dev

Optimizing the SQL Query for Calculating account balance

Related Related

  1. 1

    Calculating the sum of a variable within all children (recursive)

  2. 2

    Parent div takes height of first child element and not the sum of all children's height

  3. 3

    Parent div takes height of first child element and not the sum of all children's height

  4. 4

    SUM operation on attributes of children of multiple parent records

  5. 5

    SUM operation on attributes of children of multiple parent records

  6. 6

    d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

  7. 7

    jQuery loop through multidimensional array and display children's for each parent array

  8. 8

    Communicate a change in one of children to parent's parent

  9. 9

    PHP How to total up the sum from each balance in a foreach loop

  10. 10

    PHP how to move the children up to each parent

  11. 11

    Match heights of children within each parent

  12. 12

    Calculating scene's children's matrixes without rendering

  13. 13

    Calculating a Sum

  14. 14

    Calculating a Sum

  15. 15

    Rails: List all of Parent's children in Parent's Show

  16. 16

    Parent div not expanding to children's height

  17. 17

    Laravel parent/children relationship on it's own model

  18. 18

    Parent container's height is not stretching for children?

  19. 19

    Parent height not covering children's padding

  20. 20

    sum of cases in grand-parent, parents, children,, and etc

  21. 21

    Parent value as sum of all children values within nested javascript object

  22. 22

    Sum content values for each parent node

  23. 23

    Get count of children from each parent.SQL Server 2012

  24. 24

    XML file parsing - Get data from each parent and their own children

  25. 25

    Get each Children element that has a class and insert before its parent

  26. 26

    Use recursion to get parent value and children value and all its children's children value

  27. 27

    parent div's width larger than children's width in Firefox

  28. 28

    Calculating customer balance from QuickBooks data (QBXML)

  29. 29

    Optimizing the SQL Query for Calculating account balance

HotTag

Archive