Grouping of a many to many relationship data in a table - recursive sql?
846123Mar 9 2011 — edited Mar 9 2011Hi,
I am trying to group data that is related with a many to many relationship into a unique group number using SQL only. The table looks like this.
CHECK PLAN AMOUNT
A 1 10
A 2 15
B 2 11
B 3 12
B 4 13
C 4 16
D 5 17
The result should look like this. Since A is related to 1 and 2, and 2 is related to B and B is related to 3,and 4, 4 is related to C, all these rows should be considered one group. Any direction on where I should start looking or if there are any Oracle functions that do this will be much appreciated. The oracle version is 10g.
CHECK PLAN AMOUNT GROUPID
A 1 10 1
A 2 15 1
B 2 11 1
B 3 12 1
B 4 13 1
C 4 16 1
D 5 17 2
CREATE TABLE PAS_DBA.GRPDEL
(
CHECKID VARCHAR2(5 BYTE),
PLAN INTEGER,
AMOUNT NUMBER(6,2)
);
SET DEFINE OFF;
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('A', 1, 10);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('A', 2, 15);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('B', 2, 11);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('B', 3, 12);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('B', 4, 13);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('C', 4, 16);
Insert into PAS_DBA.GRPDEL
(CHECKID, PLAN, AMOUNT)
Values
('D', 5, 17);
COMMIT;