SQL QUERY for parent child relationship and Dynamic Compression

167 Views Asked by At

I need help writing a query for a parent-child relationship with dynamic compression.

The table here is

member_id   parent_id   fullname    username    product
1           0           Alpha       alpha       1
2           1           A           A           1
3           1           B           B           1
4           1           C           C           1
5           2           A1          A1          0
6           2           A2          A2          0
7           2           A3          A3          4
8           5           A11         A11         5
9           5           A12         A12         5
10          5           A13         A13         0
11          6           A21         A21         5
12          6           A22         A22         5
13          8           a111        a111        5
14          13          a1111       a1111       5
15          14          a11111      a11111      4
16          15          a111111     a111111     4
17          16          a1111111    a1111111    2
18          17          a11111111   a11111111   1
19          3           B1          B1          1

After processing this table there will be one field with total_product which will contain the sum of products of their first child.

And if the member doesn't have any product with hum, then the sum of the product will be added to his parent. And even if the parent doesn't have any product, then that sum will be added to his parent, and so on.

Please guide me on this

0

There are 0 best solutions below