WITH TT AS (
SELECT 10000 AS DEPTCD, '회사' AS DEPTNAME, NULL AS PARENTDEPTCD, 1 AS DEPTH
UNION ALL
SELECT 11000 AS DEPTCD, '경영지원실' AS DEPTNAME, 10000 AS PARENTDEPTCD, 2 AS DEPTH
UNION ALL
SELECT 12000 AS DEPTCD, '영업지원실' AS DEPTNAME, 10000 AS PARENTDEPTCD, 2 AS DEPTH
UNION ALL
SELECT 12100 AS DEPTCD, '영업팀' AS DEPTNAME, 12000 AS PARENTDEPTCD, 3 AS DEPTH
UNION ALL
SELECT 12200 AS DEPTCD, '영업팀' AS DEPTNAME, 12000 AS PARENTDEPTCD, 3 AS DEPTH
UNION ALL
SELECT 11100 AS DEPTCD, '경제팀' AS DEPTNAME, 11000 AS PARENTDEPTCD, 3 AS DEPTH
UNION ALL
SELECT 11200 AS DEPTCD, '경영팀' AS DEPTNAME, 11000 AS PARENTDEPTCD, 3 AS DEPTH
UNION ALL
SELECT 11210 AS DEPTCD, '총무파트' AS DEPTNAME, 11200 AS PARENTDEPTCD, 4 AS DEPTH
UNION ALL
SELECT 11220 AS DEPTCD, '인사파트' AS DEPTNAME, 11200 AS PARENTDEPTCD, 4 AS DEPTH
UNION ALL
SELECT 11110 AS DEPTCD, '경제전략1팀' AS DEPTNAME, 11100 AS PARENTDEPTCD, 4 AS DEPTH
UNION ALL
SELECT 11120 AS DEPTCD, '경제전략2팀' AS DEPTNAME, 11100 AS PARENTDEPTCD, 4 AS DEPTH
), CTE AS (
SELECT DEPTCD
, DEPTNAME
, PARENTDEPTCD
, CONVERT(NVARCHAR(300), DEPTNAME) AS FULLPATH
, CONVERT(NVARCHAR(300), DEPTNAME) AS REVERSEFULLPATH
FROM TT
WHERE PARENTDEPTCD IS NULL
UNION ALL
SELECT B.DEPTCD
, B.DEPTNAME
, B.PARENTDEPTCD
, CONVERT(NVARCHAR(300), CONCAT(C.FULLPATH, ' > ', B.DEPTNAME)) AS FULLPATH
, CONVERT(NVARCHAR(300), CONCAT(B.DEPTNAME, ' > ', C.REVERSEFULLPATH)) AS REVERSEFULLPATH
FROM TT B, CTE C
WHERE B.PARENTDEPTCD = C.DEPTCD
)
SELECT *
FROM CTE