Quantcast
Channel: Active questions tagged cte - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 207

How to merge multiple rows in one column with hierarchy Postgresql 12?

$
0
0

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 catalogALTER COLUMN parent_id SET DEFAULT nextval('seq_parent_id')

Can anyone please give a rough idea how to transform the structure of the table?


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>