Skip to main content

Hierarchical Query In SQL

it can be done in SQL SERVER 2005 and later using CTE (common table expression)

DECLARE @Table1 AS TABLE
(
LevelID INT NOT NULL PRIMARY KEY,
Position NVARCHAR(50) NOT NULL,
ReportingLevelID INT
)

INSERT INTO @Table1 SELECT 1, 'Chief Executive Officer', NULL
INSERT INTO @Table1 SELECT 2, 'Senior Director - Development', 1
INSERT INTO @Table1 SELECT 3, 'Senior Director - Finance', 1
INSERT INTO @Table1 SELECT 4, 'Senior Director - Human Resources', 1
INSERT INTO @Table1 SELECT 5, 'Product Development Manager', 2
INSERT INTO @Table1 SELECT 6, 'Project Lead', 5
INSERT INTO @Table1 SELECT 7, 'QA Lead', 5
INSERT INTO @Table1 SELECT 8, 'Documentation Lead', 5
INSERT INTO @Table1 SELECT 9, 'Developers', 6
INSERT INTO @Table1 SELECT 10, 'Testers', 7
INSERT INTO @Table1 SELECT 11, 'Writers', 8
INSERT INTO @Table1 SELECT 12, 'Accountants', 3
INSERT INTO @Table1 SELECT 13, 'HR Professionals', 4

SELECT * FROM @Table1;

WITH SampleOrgChart ([Level], Position, ReportingLevel, OrgLevel, SortKey) AS
(
-- Create the anchor query. This establishes the starting
-- point
SELECT
a.LevelID, a.Position, a.ReportingLevelID, 0,
CAST (a.LevelID AS VARBINARY(900))
FROM @Table1 a
WHERE a.Position = 'Chief Executive Officer'
UNION ALL
-- Create the recursive query. This query will be executed
-- until it returns no more rows
SELECT
a.LevelID, a.Position, a.ReportingLevelID, b.OrgLevel + 1,
CAST (b.SortKey + CAST (a.LevelID AS BINARY(4)) AS VARBINARY(900))
FROM @Table1 a
INNER JOIN SampleOrgChart b ON a.ReportingLevelID = b.[Level]
--WHERE b.OrgLevel < 1
)


SELECT * FROM SampleOrgChart ORDER BY SortKey


credit goes to Srinivas Sampath

until then

Comments

Popular posts from this blog

Crystal Report 2010 - Failed to load database information

"Failed to load database information" error when i try deploy my program with CR 2010 using TTX file as report definition this is because there is missing .dll, call "CRDB_FIELDDEF.DLL"..u need to manually copy the file to "C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86" folder then the report is loading again it said that TTX is old tech but i'm still using it because i don't need to connect the report to any DB, but until i found new one to replace :D check this link to read it [EDIT 25 Jan 2011] i think i'll using ADO.NET Dataset next time :D until then

Resolve Collation error in SQL Server

i often get this error when trying to JOIN some table Cannot resolve the collation conflict between "SQL_Latin1_General_CP850_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation. u need to use COLLATION when performing the JOIN, something like this SELECT field1 FROM table1 INNER JOIN table2 ON table1.field3 COLLATE DATABASE_DEFAULT = table2.field4 COLLATE DATABASE_DEFAULT i found this when trying to JOIN, maybe it affect other areas..until i found one see u

Session timeout problem in IIS

Hi, Long time no post, i will try to post again since i came out searching a typical solution over and over again :D Now i'm gonna post about session. Since i got this from other link so here it is So I started looking into the IIS settings and came to know that i missed two things to change : Application pool’s Idle Time-out (minutes) Session state  – State server’s Time-out (seconds) Once I have changed above settings than after Session timeout worked. So after that I have made one checklist that can help us to validate Session timeout settings, here i am sharing it with you as well so that you will also get some benefits from it : Application Pool  – Advanced Settings Menu – Process Model – Idle Time-out (minutes) Sites –  Session State  – Cookie Settings – Time-out (minutes) If you are using  State Server  or  SQL Server  to manage your session (instead of InProcess), Here is the steps to follow :    ...