-- DECLARE
DECLARE @Company AS TABLE(EmpID INT, ParentID INT, PersonName VARCHAR(100));
-- Insert Temp Records
INSERT INTO @Company(EmpID, ParentID, PersonName)
VALUES(1 , NULL , 'Maulik Dhorajia')
, (2 , NULL , 'Bhavesh Gohel')
, (3 , NULL , 'Dinesh Padhiyar')
, (4 , 2 , 'Vijay Kumar')
, (5 , 1 , 'Jitendra Makwana')
, (6 , 4 , 'Jayesh Dhobi')
, (7 , 1 , 'Shivpalsinh Jhala')
, (8 , 5 , 'Amit Patel')
, (9 , 3 , 'Abidali Suthar')
-- Default data
SELECT * FROM @Company;
-- Incorrect result which we usually find on Internet
;WITH CTECompany
AS
(
SELECT EmpID, ParentID, PersonName , 0 AS HLevel
FROM @Company
WHERE ParentID IS NULL
UNION ALL
SELECT C.EmpID, C.ParentID, C.PersonName , (CTE.HLevel + 1) AS HLevel
FROM @Company C
INNER JOIN CTECompany CTE ON CTE.EmpID = C.ParentID
WHERE C.ParentID IS NOT NULL
)
-- Misleading SQL
SELECT * FROM
(
SELECT
EmpID
, ParentID
, HLevel
, (REPLICATE( '----' , HLevel ) + PersonName) AS Person
FROM CTECompany
) AS P
ORDER BY HLevel;
-- Working Example
;WITH CTECompany
AS
(
SELECT
EmpID,
ParentID,
PersonName ,
0 AS HLevel,
CAST(RIGHT(REPLICATE('_',5) + CONVERT(VARCHAR(20),EmpID),20) AS VARCHAR(MAX)) AS OrderByField
FROM @Company
WHERE ParentID IS NULL
UNION ALL
SELECT
C.EmpID,
C.ParentID,
C.PersonName ,
(CTE.HLevel + 1) AS HLevel,
CTE.OrderByField + CAST(RIGHT(REPLICATE('_',5) + CONVERT(VARCHAR(20),C.EmpID),20) AS VARCHAR(MAX)) AS OrderByField
FROM @Company C
INNER JOIN CTECompany CTE ON CTE.EmpID = C.ParentID
WHERE C.ParentID IS NOT NULL
)
-- Working Example
SELECT
EmpID
, ParentID
, HLevel
, PersonName
, (REPLICATE( '----' , HLevel ) + PersonName) AS Person
FROM CTECompany
ORDER BY OrderByField,PersonName;
DECLARE @Company AS TABLE(EmpID INT, ParentID INT, PersonName VARCHAR(100));
-- Insert Temp Records
INSERT INTO @Company(EmpID, ParentID, PersonName)
VALUES(1 , NULL , 'Maulik Dhorajia')
, (2 , NULL , 'Bhavesh Gohel')
, (3 , NULL , 'Dinesh Padhiyar')
, (4 , 2 , 'Vijay Kumar')
, (5 , 1 , 'Jitendra Makwana')
, (6 , 4 , 'Jayesh Dhobi')
, (7 , 1 , 'Shivpalsinh Jhala')
, (8 , 5 , 'Amit Patel')
, (9 , 3 , 'Abidali Suthar')
-- Default data
SELECT * FROM @Company;
-- Incorrect result which we usually find on Internet
;WITH CTECompany
AS
(
SELECT EmpID, ParentID, PersonName , 0 AS HLevel
FROM @Company
WHERE ParentID IS NULL
UNION ALL
SELECT C.EmpID, C.ParentID, C.PersonName , (CTE.HLevel + 1) AS HLevel
FROM @Company C
INNER JOIN CTECompany CTE ON CTE.EmpID = C.ParentID
WHERE C.ParentID IS NOT NULL
)
-- Misleading SQL
SELECT * FROM
(
SELECT
EmpID
, ParentID
, HLevel
, (REPLICATE( '----' , HLevel ) + PersonName) AS Person
FROM CTECompany
) AS P
ORDER BY HLevel;
-- Working Example
;WITH CTECompany
AS
(
SELECT
EmpID,
ParentID,
PersonName ,
0 AS HLevel,
CAST(RIGHT(REPLICATE('_',5) + CONVERT(VARCHAR(20),EmpID),20) AS VARCHAR(MAX)) AS OrderByField
FROM @Company
WHERE ParentID IS NULL
UNION ALL
SELECT
C.EmpID,
C.ParentID,
C.PersonName ,
(CTE.HLevel + 1) AS HLevel,
CTE.OrderByField + CAST(RIGHT(REPLICATE('_',5) + CONVERT(VARCHAR(20),C.EmpID),20) AS VARCHAR(MAX)) AS OrderByField
FROM @Company C
INNER JOIN CTECompany CTE ON CTE.EmpID = C.ParentID
WHERE C.ParentID IS NOT NULL
)
-- Working Example
SELECT
EmpID
, ParentID
, HLevel
, PersonName
, (REPLICATE( '----' , HLevel ) + PersonName) AS Person
FROM CTECompany
ORDER BY OrderByField,PersonName;
Comments
Post a Comment