I've the following table:
create table T (
idGeo INT IDENTITY(1,1),
GEO VARCHAR(64),
PARENTID INT
);
insert into T (GEO, PARENTID) values
( 'EMEA', NULL),
( 'France', 1),
( 'mIDCAPSfRANCE', 2),
( 'Germany', 1),
( 'France exl midcaps', 2),
( 'Amercias', NULL),
( 'US', 6);
I'd like to get hierarchy in separated columns.
Here what I tried https://sqlize.online/sql/mssql2017/7f34918507bae9d9b74af96c5f5e83dc/
select T.idGeo, T.GEO, T1.GEO [GEO Level 1], T2.GEO [GEO Level 2]
from T
left join T T1 on T.PARENTID = T1.idGeo
left join T T2 on T1.PARENTID = T2.idGeo;
The issue is for example line 1 I suppose to get geo level1 EMEA, but I get null. How can I correct it?
