oracle11g - SQL: Add values according to index columns only for lines sharing an id -


yesterday asked question: sql: how add values according index columns found out problem bit more complicated:

i have array this


id | value|   position | relates_to_position |type  19 | 100  |       2    |        null         | 1   19 |  50  |       6    |        null         | 2   19 |  20  |       7    |           6         | 3   20 |  30  |       3    |        null         | 2   20 |  10  |       4    |           3         | 3   

from need create resulting table, adds lines relates_to_position value matches position value, lines sharing same id!

the resulting table should


id | value|   position |type   19 | 100  |       2    |  1   19 |  70  |       6    |  2   20 |  40  |       3    |  2   

i using oracle 11. there 1 level of recursion, meaning line not refer line has relates_to_pos field set.

i think following query this:

select id, coalesce(relates_to_position, position) position,        sum(value) value, min(type) type t group id, coalesce(relates_to_position, position); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -