HarborObjects

Hierarchy of a self referencing table

by AllenBerezovsky 20. February 2009 04:47

A former colleague of mine called me the other day with a following problem. She needed to write some reports against a self referencing table. Her initial thought was that she needed to create a number of temp tables, and fill them with the data for each hierarchy level. Her problem also was that she had no idea how deep the hierarchy went. When she got tired of running all kinds of select statements to find out the depth of that hierarchy, she called me for advice. Here is the sample code that I sent her.

Feel free to run this against the AdventureWorks database, and check out the results. The HumanResources.Employee table has an EmployeeID column, and a ManagerID column that references the EmployeeID within the hierarchy. The only catch is, this will not work in SQL 2000 or below.

   1:  WITH CorpStructure(ManagerID,EmployeeID, Title, Level)
   2:   AS
   3:    (
   4:    SELECT Emp.ManagerID, Emp.EmployeeID, Emp.Title, 1 as Level
   5:    FROM HumanResources.Employee as Emp
   6:    WHERE ManagerID is NULL
   7:    UNION ALL
   8:    SELECT Emp.ManagerID, Emp.EmployeeID, Emp.Title, Corp.Level + 1
   9:    FROM HumanResources.Employee as Emp INNER JOIN CorpStructure as Corp
  10:    ON Emp.ManagerID = Corp.EmployeeID
  11:   )
  12:   SELECT ManagerID, EmployeeID, Title, Level
  13:   FROM CorpStructure

Tags:

SQL Server

Code Camp Presentations Posted

by RezaMadani 19. February 2009 04:28

Sorry it took a bit longer than expected. Slide decks and code samples for my presentations at Code Camp 2009 in Fullerton (WPF Animation Techniques and Sql Server 2008 New Features) are uploaded ... Have fun.

CodeCamp Jan2009 - SQL Server 2008 - intro.zip (273.80 kb)

CodeCamp Jan2009 -WPF - animation.zip (150.35 kb)

Tags: , ,

SQL Server | Windows Presentation Foundation | WPF

Silverlight 2.0 HOL

by RezaMadani 6. February 2009 04:09

If you attended any of the Hands On Labs for Sivlerlight 2.0, you will find a few minor errors in the documentation that will slow you down or confuse you.  I have done my best to capture them in this document: “Silverlight hand on lab notes.pdf”,  Use it alongside the documentation provided by the lab.  This would require you to register on the site.

Silverlight hands on labs notes.pdf (78.23 kb)

Tags:

Silverlight