Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

Common Table Expressions

Today I was delighted to learn about common table expressions in SQL Server 2005. Essentially, CTEs replace the need for cursors when executing statements against rows in a result set. Here are some examples...

Let us assume that we have an employee table, and the employee table lists all employees in the company. Each employee has a unique ID - EmployeeID, which is the primary key in the table. Each employee reports to one manager and this is indicated by the managers EmployeeID in the ManagerID column for a given employee. e.g

EmployeeID Name ManagerID
1 Bob (CEO) NULL
2 Sally 1
3 Jim 1
4 Paul 2
5 Simon 4

As shown above, Bob is the CEO and reports to nobody, Sally and Jim report to Bob, Paul reports to Sally, and Simon reports to Paul.

Let's say that we want to list how many employees report to each manager. We could write a query using the group by clause (assuming we don't want to join the result set back with the employees table to match the name with EmployeeID), as follows:


What if we wanted to display those managers that had more than one person reporting to them? This result could be obtained by storing the results of the previous query in a temporary table and then running a separate query, or using a cursor. However, using a CTE it is possible to gain the same result with one statement, as follows:


How about listing the company hierarchy in as a tabular list, indicating rank, starting with the CEO, and following with the direct reports at each level throughout the company? A CTE is great for this problem:


The last example uses recursion to produce the ranking. This query will work no matter how many employees work for the company and no matter how many levels of management. Prior to CTEs, obtaining the same result would have involved some sophisticated SQL.

Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Friday, May 20, 2005 4:45 PM by Rob Garrett

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems