Hello,
I am facing a problem that occasionally comes up when you deal with not fully normalized table. Here is the problem. Imagine a table with 4 columns, and let's call this table dbo.Hierarchical. Here is the definition of the table:
if OBJECT_ID('dbo.Hierarchical') is not null
drop table dbo.Hierarchical
create table dbo.Hierarchical
(
colID int not null identity(1,1) primary key
,GroupName varchar(5) not null
,IsAtomic bit not null
,Constituent varchar(5) null
)
This table can have a GroupName that is Atomic, which means that it doesn not have a component, or can not be Atomic. In this case, a GroupName can contain other GroupNames.
Lets fill the table with some data for clarity.
set nocount on
insert into dbo.Hierarchical values ('A',0,'B')
insert into dbo.Hierarchical values ('A',0,'C')
insert into dbo.Hierarchical values ('B',1,'B')
insert into dbo.Hierarchical values ('C',0,'K')
insert into dbo.Hierarchical values ('C',0,'L')
insert into dbo.Hierarchical values ('D',0,'E')
insert into dbo.Hierarchical values ('D',0,'F')
insert into dbo.Hierarchical values ('D',0,'G')
insert into dbo.Hierarchical values ('E',1,'E')
insert into dbo.Hierarchical values ('F',1,'F')
insert into dbo.Hierarchical values ('G',0,'H')
insert into dbo.Hierarchical values ('G',0,'I')
insert into dbo.Hierarchical values ('H',1,'H')
insert into dbo.Hierarchical values ('I',1,'I')
insert into dbo.Hierarchical values ('J',1,'J')
insert into dbo.Hierarchical values ('K',1,'K')
insert into dbo.Hierarchical values ('L',1,'L')
insert into dbo.Hierarchical values ('M',1,'M')
insert into dbo.Hierarchical values ('N',1,'N')
set nocount off
Now if we look at a simple select * from dbo.Hierarchical we get the following:
GroupName colID IsAtomic Constituent
A 1 0 B
A 2 0 C
B 3 1 B
C 4 0 K
C 5 0 L
D 6 0 E
D 7 0 F
D 8 0 G
E 9 1 E
F 10 1 F
G 11 0 H
G 12 0 I
H 13 1 H
I 14 1 I
J 15 1 J
K 16 1 K
L 17 1 L
M 18 1 M
N 19 1 N
Whew, that was long winded. Now, notice that the first two rows have GroupName A and Constiuents B and C. B is Atomic, so it has no further constiuents. C, however, has constiuents K, L (K and L are Atomic). How can I create a view that will flatten this table out so that I only see GroupName and the Atomic constiuents. In the case of GroupName A, I shoud see 3 rows
A B
A K
A L