I have got a table with columns (srno, Name, Product, Amount).
The values are
(1, Ronak, Iphone, 40000),
(2, Ronak, Iphone, 36000),
(3, Ronak, Iphone, 38000),
(4, Naman, Iphone, 40000),
(5, Naman, Ipad, 20000),
(6, Nihar, Ipad, 20000),
(7, Ronak, Ipad, 19000),
(8, Naman, Iphone, 37000),
(9, Nihar, Ipad, 40000),
I want to fetch the distinct values and store it in another table with their respective parent_id. The output should be like below:
(uid, name, parent_id)
(1, Ronak, NULL),
(2, Naman, NULL),
(3, Nihar, NULL),
(4, Iphone, 1),
(5, Iphone, 2),
(6, Ipad, 2),
(7, Ipad, 3),
(8, Ipad, 1),
(9, 40000, 4),
(10, 36000, 4),
(11, 38000, 4),
(12, 40000, 5),
(13, 20000, 6),
(14, 20000, 7),
(15, 19000, 8),
(16, 37000, 5),
(17, 40000, 7),
The parent_id
is the uid
from the same table
Can anyone tell me what query in postgresql can achieve me the desired output. I am using PHP and PostgreSQL.