EmployeeId 109 (CEO) EmployeeId 6 (Marketing Manager) EmployeeId 12 (VP Engineering) EmployeeId 42 (IS Manager) EmployeeId 140 (CFO) EmployeeId 148 (VP Production) EmployeeId 273 (VP Sales)
EmployeeId 6 (Marketing Manager) EmployeeId 12 (VP Engineering) EmployeeId 42 (IS Manager) EmployeeId 140 (CFO) EmployeeId 148 (VP Production) EmployeeId 273 (VP Sales)
6: 109.6 12: 109.12 42: 109.42 140: 109.140 148: 109.148 273: 109.273
6: 1.1 12: 1.2 42: 1.3 140: 1.4 148: 1.5 273: 1.6
WITH EmployeeRows AS ( SELECT EmployeeId, ManagerId, ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow FROM HumanResources.Employee WHERE ManagerId IS NULL UNION ALL SELECT e.EmployeeId, e.ManagerId, ROW_NUMBER() OVER (ORDER BY e.EmployeeId) AS theRow FROM EmployeeRows x JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId ) SELECT * FROM EmployeeRows ORDER BY ManagerId, EmployeeId
SELECT EmployeeId, ManagerId, ROW_NUMBER() OVER (PARTITION BY ManagerId ORDER BY EmployeeId) AS theRow FROM HumanResources.Employee ORDER BY ManagerId, EmployeeId
WITH EmployeeRows AS ( SELECT EmployeeId, ManagerId, CONVERT(VARCHAR(MAX), ROW_NUMBER() OVER (ORDER BY EmployeeId)) AS thePath FROM HumanResources.Employee WHERE ManagerId IS NULL UNION ALL SELECT e.EmployeeId, e.ManagerId, x.thePath + '.' + CONVERT(VARCHAR(MAX), ROW_NUMBER() OVER (ORDER BY e.EmployeeId)) AS thePath FROM EmployeeRows x JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId ) SELECT * FROM EmployeeRows ORDER BY thePath
WITH EmployeeRows AS ( SELECT y.EmployeeId, y.ManagerId, CONVERT(VARCHAR(MAX), y.theRow) AS thePath FROM ( SELECT EmployeeId, ManagerId, ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow FROM HumanResources.Employee WHERE ManagerId IS NULL ) y UNION ALL SELECT y.EmployeeId, y.ManagerId, y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath FROM ( SELECT e.EmployeeId, e.ManagerId, x.thePath, ROW_NUMBER() OVER (ORDER BY e.EmployeeId) AS theRow FROM EmployeeRows x JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId ) y ) SELECT * FROM EmployeeRows ORDER BY thePath
WITH EmployeeRows AS ( SELECT EmployeeId, CONVERT(VARCHAR(MAX), theRow) AS thePath, CONVERT(BIGINT, 1) AS prevNumer, CONVERT(BIGINT, 0) AS prevDenom, CONVERT(BIGINT, theRow) AS currNumer, CONVERT(BIGINT, 1) AS currDenom FROM ( SELECT EmployeeId, ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow FROM HumanResources.Employee WHERE ManagerId IS NULL ) y UNION ALL SELECT y.EmployeeId, y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath, prevNumer = y.currNumer, prevDenom = y.currDenom, (y.currNumer * y.theRow) + y.prevNumer AS currNumer, (y.currDenom * y.theRow) + y.prevDenom AS currDenom FROM ( SELECT e.EmployeeID, x.thePath, x.currNumer, x.currDenom, x.prevNumer, x.prevDenom, ROW_NUMBER() OVER (ORDER BY e.EmployeeID) AS therow FROM EmployeeRows x JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId ) y )
WITH EmployeeRows AS ( SELECT EmployeeId, CONVERT(VARCHAR(MAX), theRow) AS thePath, CONVERT(BIGINT, 1) AS prevNumer, CONVERT(BIGINT, 0) AS prevDenom, CONVERT(BIGINT, theRow) AS currNumer, CONVERT(BIGINT, 1) AS currDenom FROM ( SELECT EmployeeId, ROW_NUMBER() OVER (ORDER BY EmployeeId) AS theRow FROM HumanResources.Employee WHERE ManagerId IS NULL ) y UNION ALL SELECT y.EmployeeId, y.thePath + '.' + CONVERT(VARCHAR(MAX), y.theRow) AS thePath, prevNumer = y.currNumer, prevDenom = y.currDenom, (y.currNumer * y.theRow) + y.prevNumer AS currNumer, (y.currDenom * y.theRow) + y.prevDenom AS currDenom FROM ( SELECT e.EmployeeID, x.thePath, x.currNumer, x.currDenom, x.prevNumer, x.prevDenom, ROW_NUMBER() OVER (ORDER BY e.EmployeeID) AS therow FROM EmployeeRows x JOIN HumanResources.Employee e ON e.ManagerId = x.EmployeeId ) y ) SELECT EmployeeId, thePath, currNumer AS startNumer, currDenom AS startDenom, currNumer + prevNumer AS endNumer, currDenom + prevDenom AS endDenom FROM EmployeeRows
September 24/25, 2005
Register Today
September 24 – 8:30 AM – 9PM
September 25 – 8:30 AM – 4PM
Microsoft Waltham201 Jones Rd.
Waltham, Ma.
Call for Speakers
Are you a developer interested in improving your .NET skills? Then this is the event to attend. Code Camp 4: Developers Gone Wild promises to be both bigger and better than anything we have done before. This free two day seminar is designed as a series of intensive code related demos and technical sessions to guide the developer to the next skill level. The continuing goal of the Code Camps is to provide an intensive developer to developer learning experience that is fun and technically stimulating. The focus is on delivering programming information and sample code that can be used immediately. All training, slides, manuals and demo code is provided free!
This two day camp is hosted in our Waltham facility. The leading technical camp counselors from both Microsoft and the New England Developer Community will share their technical expertise and experiences. This code camp is divided into three tracks – Smart Client, Web Development and Data Technologies. Each track starts with a “get the code” basics before advancing to more advanced topics.
WITH EmployeeLevels AS ( SELECT EmployeeId, CONVERT(VARCHAR(MAX), EmployeeId) AS thePath, 1 AS Level FROM HumanResources.Employee WHERE ManagerId IS NULL UNION ALL SELECT e.EmployeeId, x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath, x.Level + 1 AS Level FROM EmployeeLevels x JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId )
WITH EmployeeLevels AS ( SELECT EmployeeId, CONVERT(VARCHAR(MAX), EmployeeId) AS thePath, 1 AS Level FROM HumanResources.Employee WHERE ManagerId IS NULL UNION ALL SELECT e.EmployeeId, x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath, x.Level + 1 AS Level FROM EmployeeLevels x JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId ), EmployeeRows AS ( SELECT EmployeeLevels.*, ROW_NUMBER() OVER (ORDER BY thePath) AS Row FROM EmployeeLevels )
WITH EmployeeLevels AS ( SELECT EmployeeId, CONVERT(VARCHAR(MAX), EmployeeId) AS thePath, 1 AS Level FROM HumanResources.Employee WHERE ManagerId IS NULL UNION ALL SELECT e.EmployeeId, x.thePath + '.' + CONVERT(VARCHAR(MAX), e.EmployeeId) AS thePath, x.Level + 1 AS Level FROM EmployeeLevels x JOIN HumanResources.Employee e on e.ManagerId = x.EmployeeId ), EmployeeRows AS ( SELECT EmployeeLevels.*, ROW_NUMBER() OVER (ORDER BY thePath) AS Row FROM EmployeeLevels ) SELECT ER.EmployeeId, ER.thePath, ER.Level, ER.Row, (ER.Row * 2) - ER.Level AS Lft, ((ER.Row * 2) - ER.Level) + ( SELECT COUNT(*) * 2 FROM EmployeeRows ER2 WHERE ER2.thePath LIKE ER.thePath + '.%' ) + 1 AS Rgt FROM EmployeeRows ER ORDER BY thePath
Just announced on Thom Robbins' blog, I'm doing a SQL Server Mini-Code Camp on October 22 at the Microsoft offices in Waltham, MA. This will be a full day of content.
The morning session will focus on data fundamentals and set-based thinking. This will include a study of First Normal Form and some brief discussion of higher normal forms, a very basic intro to referential integrity, and an introduction to 3-valued logic and NULLs. I'll then proceed into set-based vs. procedural thinking, and show how to write better queries without using loops or cursors.
The afternoon session will be a deep dive into SQL Server 2005 CLR integration. I'll cover primarily how to program--and best practices for using--CLR UDTs, UDFs, aggregates, and stored procedures. I'll also briefly discuss triggers, but honestly I don't see much value in that feature so I'm going to spend very little time discussing them.
I'm very excited to be doing this! Let me know (soon, please) if you have any specific areas you'd like covered and I'll see if I can work it into the content.