I am trying to get all related ancestors of a child with more info by joining with another table. I'm kind of new to backend so recursive cte is hard to understand.
Find SQL Fiddle here
Data I have : product_id,user_id
Data I need :
| user_id | master_id | cost | 
|---|---|---|
| id(john_snow) | null | 4 | 
| id(bran_stark) | id(john_snow) | 6 | 
| id(arya_stark) | id(bran_stark) | 8 | 
| id(sansa_stark) | id(arya_stark) | 10 | 
So that I can add profits to respective users and least cost as company profit.
 
    