I have a table like this to save the catalog of 90 services:
-------------------------------------------------------------------------------------
| id | service | subservice | description | cost
-------------------------------------------------------------------------------------
| 2044 | Tests | Tests | Calcium | 50.00
| 1385 | Cardiology | Cardioversion | Electric Cardioversion programmed| 200.00
| 7000 | Cardiology | Ecocardiography| Chest Ultrasound | 100.00
-------------------------------------------------------------------------------------
I need to change the table structure in order to have the three levels (service, subservice and description) in the same column with its own id and pointing to a new column with the level's number. Namely something like this (note that the id's are something that I made up):
-------------------------------------------------------------------------------------
| id | description | parent_id | cost
-------------------------------------------------------------------------------------
| 1 | Tests | NULL | 0.00
| 2 | Tests | 1 | 0.00
| 2044 | Calcium | 2 | 50.00
-------------------------------------------------------------------------------------
I'm working on Postgresql 12. I have created the new column parent_id and I was trying to do this sequence:
CREATE SEQUENCE seq_parent_id INCREMENT BY 1 START WITH 1 NO CYCLE;
ALTER TABLE catalog
ALTER COLUMN parent_id SET DEFAULT nextval('seq_parent_id')
Can anyone please give a rough idea how to transform the structure of the table?
sequenceon parent_id table. Sencond do you want to store the existing data into new transformed table?