add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
WITH
links_TABLE (Parent_id, Child_id, Link_id, ChildType) AS
(
VALUES
('001', '002', '201', 'Group')
, ('001', '101', '202', 'People')
, ('001', '102', '203', 'People')
, ('002', '003', '204', 'Group')
, ('002', '004', '205', 'Group')
, ('002', '103', '206', 'People')
, ('003', '104', '207', 'People')
, ('004', '105', '208', 'People')
)
, TTEMP (Parent_id, Child_id, Link_id, ChildType, Parent_id_Parent, Link_id_Parent) AS
(
SELECT Parent_id, Child_id, Link_id, ChildType, '000' Parent_id_Parent, '000' Link_id_Parent
FROM links_TABLE C
WHERE NOT EXISTS (SELECT 1 FROM links_TABLE P WHERE P.CHILD_ID = C.PARENT_ID)
UNION ALL
SELECT X.Parent_id, X.Child_id, X.Link_id, X.ChildType, P.Parent_id, P.Link_id
FROM TTEMP P, links_TABLE X
WHERE X.Parent_id = P.Child_id
)
SELECT *
FROM TTEMP;
PARENT_ID CHILD_ID LINK_ID CHILDTYPE PARENT_ID_PARENT LINK_ID_PARENT
001 002 201 Group 000 000
001 101 202 People 000 000
001 102 203 People 000 000
002 003 204 Group 001 201
002 004 205 Group 001 201
002 103 206 People 001 201
003 104 207 People 002 204
004 105 208 People 002 205