Pages

Thursday, March 22, 2012

How to design an organization chart?


The following T-SQL database scripts in Microsoft SQL Server Management Studio Query Editor demonstrate the building of an organizational chart for AdventureWorks & AdventureWorks2008 databases of AdventureWorks Cycles, the fictional bike company invented by Microsoft staff.

-- SQL recursive CTE to find subordinates of executive, manager & supervisor
-- Tree processing - find all descendants - find children - self-referencing table
DECLARE  @EmployeeID INT 
SET @EmployeeID = 109 
USE AdventureWorks; 
WITH cteEmployeeName 
     AS (SELECT FullName = FirstName + ' ' + LastName, 
                EmployeeID 
         FROM   HumanResources.Employee e 
                INNER JOIN Person.Contact c 
                  ON e.ContactID = c.ContactID), 
     cteSubordinates 
     AS (SELECT EmployeeID, 
                LEVEL = 0 
         FROM   HumanResources.Employee 
         WHERE  EmployeeID = @EmployeeID 
         UNION ALL 
         SELECT e.EmployeeID, 
                LEVEL + 1 
         FROM   cteSubordinates cte 
                INNER JOIN HumanResources.Employee e 
                  ON cte.EmployeeID = e.ManagerID) 
SELECT FullName, 
       s.EmployeeID, 
       LEVEL 
FROM   cteSubordinates s 
       INNER JOIN cteEmployeeName en 
         ON s.EmployeeID = en.EmployeeID 
/*
FullName          EmployeeID  Level
Ken Sánchez       109         0
David Bradley     6           1
Terri Duffy       12          1
Jean Trenary      42          1
Laura Norman      140         1
James Hamilton    148         1
Brian Welcker     273         1
Stephen Jiang     268         2
Amy Alberts       284         2
*/
------------
-- SQL Server 2008 tree processing functions - MSSQL organization chart - orgchart
-- T-SQL recursive Common Table Expression - CTE - SQL hierarchyid
USE AdventureWorks2008; 
GO 
WITH cteDirectReports(ManagerID,EmployeeID,ManagerLevel) 
     AS (SELECT OrganizationNode.GetAncestor(1), 
                OrganizationNode, 
                OrganizationLevel - 1 
         FROM   HumanResources.Employee 
         WHERE  OrganizationLevel = 0 
         UNION ALL 
         SELECT e.OrganizationNode.GetAncestor(1), 
                e.OrganizationNode, 
                OrganizationLevel - 1 
         FROM   HumanResources.Employee e 
                INNER JOIN cteDirectReports d 
                  ON e.OrganizationNode.GetAncestor(1) = d.EmployeeID) 
SELECT   Manager = replicate('_',(ManagerLevel) * 4) + CO.LastName + ', ' +
                   CO.FirstName, 
         Employee = C.LastName + ', ' + C.FirstName, 
         ManagerLevel, 
         EmployeeLevel = ManagerLevel + 1 
FROM     cteDirectReports DR 
         INNER JOIN HumanResources.Employee E 
           ON DR.EmployeeID = E.OrganizationNode 
         INNER JOIN Person.Person C 
           ON E.BusinessEntityID = C.BusinessEntityID 
         INNER JOIN HumanResources.Employee EM 
           ON DR.ManagerID = EM.OrganizationNode 
         INNER JOIN Person.Person CO 
           ON EM.BusinessEntityID = CO.BusinessEntityID 
ORDER BY DR.EmployeeID
GO
-- Results
ManagerEmployeeManagerLevelEmployeeLevel
Sánchez, KenDuffy, Terri01
____Duffy, TerriTamburello, Roberto12
________Tamburello, RobertoWalters, Rob23
________Tamburello, RobertoErickson, Gail23
________Tamburello, RobertoGoldberg, Jossef23
________Tamburello, RobertoMiller, Dylan23
____________Miller, DylanMargheim, Diane34
____________Miller, DylanMatthew, Gigi34
____________Miller, DylanRaheem, Michael34
________Tamburello, RobertoCracium, Ovidiu23
____________Cracium, OvidiuD'Hers, Thierry34
____________Cracium, OvidiuGalvin, Janice34
________Tamburello, RobertoSullivan, Michael23
________Tamburello, RobertoSalavaria, Sharon23
Sánchez, KenBradley, David01
____Bradley, DavidBrown, Kevin12
____Bradley, DavidWood, John12
____Bradley, DavidDempsey, Mary12
____Bradley, DavidBenshoof, Wanida12
____Bradley, DavidEminhizer, Terry12
____Bradley, DavidHarnpadoungsataya, Sariya12
____Bradley, DavidGibson, Mary12
____Bradley, DavidWilliams, Jill12
Sánchez, KenHamilton, James01
____Hamilton, JamesKrebs, Peter12
________Krebs, PeterBrown, Jo23
____________Brown, JoGilbert, Guy34
____________Brown, JoMcArthur, Mark34
____________Brown, JoSimon, Britta34
____________Brown, JoShoop, Margie34
____________Brown, JoLaszlo, Rebecca34
____________Brown, JoStahl, Annik34
____________Brown, JoMohan, Suchitra34
____________Brown, JoHeidepriem, Brandon34
____________Brown, JoLugo, Jose34
____________Brown, JoOkelberry, Chris34
____________Brown, JoAbercrombie, Kim34
____________Brown, JoDudenhoefer, Ed34
________Krebs, PeterDobney, JoLynn23
____________Dobney, JoLynnBaker, Bryan34
____________Dobney, JoLynnKramer, James34
____________Dobney, JoLynnAnderson, Nancy34
____________Dobney, JoLynnRapier, Simon34
____________Dobney, JoLynnMichaels, Thomas34
____________Dobney, JoLynnKogan, Eugene34
________Krebs, PeterHill, Andrew23
____________Hill, AndrewEllerbrock, Ruth34
____________Hill, AndrewJohnson, Barry34
____________Hill, AndrewHiga, Sidney34
____________Hill, AndrewFord, Jeffrey34
____________Hill, AndrewHartwig, Doris34
____________Hill, AndrewGlimp, Diane34
____________Hill, AndrewKearney, Bonnie34
________Krebs, PeterMaxwell, Taylor23
____________Maxwell, TaylorSmith, Denise34
____________Maxwell, TaylorMiller, Frank34
____________Maxwell, TaylorKeil, Kendall34
____________Maxwell, TaylorHohman, Bob34
____________Maxwell, TaylorMale, Pete34
____________Maxwell, TaylorTibbott, Diane34
________Krebs, PeterCampbell, John23
____________Campbell, JohnDusza, Maciej34
____________Campbell, JohnZwilling, Michael34
____________Campbell, JohnReeves, Randy34
____________Campbell, JohnKhanna, Karan34
____________Campbell, JohnAdams, Jay34
____________Campbell, JohnFitzgerald, Charles34
____________Campbell, JohnMasters, Steve34
____________Campbell, JohnOrtiz, David34
________Krebs, PeterRay, Michael23
____________Ray, MichaelSelikoff, Steven34
____________Ray, MichaelPoland, Carole34
____________Ray, MichaelRettig, Bjorn34
____________Ray, MichaelOsada, Michiko34
____________Ray, MichaelPhilips, Carol34
____________Ray, MichaelNetz, Merav34
________Krebs, PeterD'sa, Reuben23
____________D'sa, ReubenBrown, Eric34
____________D'sa, ReubenKaliyath, Sandeep34
____________D'sa, ReubenFrintu, Mihail34
____________D'sa, ReubenCreasey, Jack34
____________D'sa, ReubenCook, Patrick34
____________D'sa, ReubenMartinez, Frank34
____________D'sa, ReubenGoldstein, Brian34
____________D'sa, ReubenCornelsen, Ryan34
________Krebs, PeterPetculescu, Cristian23
____________Petculescu, CristianStadick, Betsy34
____________Petculescu, CristianWedge, Patrick34
____________Petculescu, CristianTiedt, Danielle34
____________Petculescu, CristianZimmerman, Kimberly34
____________Petculescu, CristianVande Velde, Tom34
________Krebs, PeterLoh, Kok-Ho23
____________Loh, Kok-HoHunter, Russell34
____________Loh, Kok-HoScardelis, Jim34
____________Loh, Kok-HoKeyser, Elizabeth34
____________Loh, Kok-HoSamant, Mandar34
____________Loh, Kok-HoTejani, Sameer34
____________Loh, Kok-HoYu, Nuan34
____________Loh, Kok-HoSong, Lolan34
____________Loh, Kok-HoPournasseh, Houman34
________Krebs, PeterMu, Zheng23
____________Mu, ZhengErsan, Ebru34
____________Mu, ZhengBaker, Mary34
____________Mu, ZhengHomer, Kevin34
____________Mu, ZhengKane, John34
____________Mu, ZhengHill, Christopher34
________Krebs, PeterLiu, Jinghao23
____________Liu, JinghaoCiccu, Alice34
____________Liu, JinghaoCao, Jun34
____________Liu, JinghaoFatima, Suroor34
____________Liu, JinghaoEvans, John34
____________Liu, JinghaoMoschell, Linda34
____________Liu, JinghaoKrapauskas, Mindaugas34
____________Liu, JinghaoBarbariol, Angela34
____________Liu, JinghaoPatten, Michael34
____________Liu, JinghaoNiswonger, Chad34
____________Liu, JinghaoHall, Don34
____________Liu, JinghaoEntin, Michael34
____________Liu, JinghaoLertpiriyasuwat, Kitti34
________Krebs, PeterAckerman, Pilar23
____________Ackerman, PilarEaton, Susan34
____________Ackerman, PilarKuppa, Vamsi34
____________Ackerman, PilarRalls, Kim34
____________Ackerman, PilarBerndt, Matthias34
____________Ackerman, PilarBischoff, Jimmy34
________Krebs, PeterHamilton, David23
____________Hamilton, DavidKomosinski, Paul34
____________Hamilton, DavidYukish, Gary34
____________Hamilton, DavidCaron, Rob34
____________Hamilton, DavidCetinok, Baris34
____________Hamilton, DavidHolliday, Nicole34
____________Hamilton, DavidRothkugel, Michael34
________Krebs, PeterGubbels, Eric23
____________Gubbels, EricSalmre, Ivo34
____________Gubbels, EricValdez, Sylvester34
____________Gubbels, EricSousa, Anibal34
____________Gubbels, EricSmith, Samantha34
____________Gubbels, EricTing, Hung-Fu34
____________Gubbels, EricSamarawickrama, Prasanna34
____________Gubbels, EricSu, Min34
____________Gubbels, EricTurner, Olinda34
____________Gubbels, EricSunkammurali, Krishna34
____________Gubbels, EricSingh, Paul34
________Krebs, PeterRandall, Cynthia23
____________Randall, CynthiaWang, Jian Shuo34
____________Randall, CynthiaReátegui Alayo, Sandra34
____________Randall, CynthiaWatters, Jason34
____________Randall, CynthiaRuth, Andy34
____________Randall, CynthiaVanderhyde, Michael34
____________Randall, CynthiaShabalin, Rostislav34
________Krebs, PeterLi, Yuhong23
____________Li, YuhongFeng, Hanying34
____________Li, YuhongSam, Raymond34
____________Li, YuhongFakhouri, Fadi34
____________Li, YuhongSacksteder, Lane34
____________Li, YuhongRandall, Linda34
____________Li, YuhongDyck, Shelley34
____________Li, YuhongEarls, Terrence34
________Krebs, PeterHay, Jeff23
____________Hay, JeffKoenigsbauer, Kirk34
____________Hay, JeffSteele, Laura34
____________Hay, JeffNayberg, Alex34
____________Hay, JeffCencini, Andrew34
____________Hay, JeffPreston, Chris34
________Krebs, PeterRichins, Jack23
____________Richins, JackJohnson, David34
____________Richins, JackYoung, Garrett34
____________Richins, JackMetters, Susan34
____________Richins, JackLi, George34
____________Richins, JackYalovsky, David34
____________Richins, JackIngle, Marc34
____________Richins, JackZabokritski, Eugene34
____________Richins, JackMartin, Benjamin34
____________Richins, JackKoch, Reed34
____________Richins, JackLawrence, David34
____________Richins, JackKing, Russell34
____________Richins, JackFrum, John34
____________Richins, JackMiksovsky, Jan34
________Krebs, PeterMcAskill-White, Katie23
____________McAskill-White, KatieHines, Michael34
____________McAskill-White, KatieMirchandani, Nitin34
____________McAskill-White, KatieDecker, Barbara34
____________McAskill-White, KatieChen, John34
____________McAskill-White, KatieHesse, Stefen34
________Krebs, PeterKim, Shane23
____________Kim, ShaneMcKay, Yvonne34
____________Kim, ShaneHite, Douglas34
____________Kim, ShaneEsteves, Janeth34
____________Kim, ShaneRounthwaite, Robert34
____________Kim, ShanePenuchot, Lionel34
________Krebs, PeterDiaz, Brenda23
____________Diaz, BrendaMcGuel, Alejandro34
____________Diaz, BrendaNorthup, Fred34
____________Diaz, BrendaLiu, Kevin34
____________Diaz, BrendaMohamed, Shammi34
____________Diaz, BrendaPatel, Rajesh34
____________Diaz, BrendaNay, Lorraine34
____________Diaz, BrendaNartker, Paula34
____________Diaz, BrendaLee, Frank34
____________Diaz, BrendaLloyd, Brian34
____________Diaz, BrendaNusbaum, Tawana34
____________Diaz, BrendaMyer, Ken34
____________Diaz, BrendaMares, Gabe34
________Krebs, PeterKane, Lori23
____________Kane, LoriMunson, Stuart34
____________Kane, LoriAlderson, Greg34
____________Kane, LoriGode, Scott34
____________Kane, LoriFlood, Kathie34
____________Kane, LoriNewman, Belinda34
____Hamilton, JamesAbolrous, Hazem12
________Abolrous, HazemWu, Peng23
____________Wu, PengCharncherngkha, Sootha34
____________Wu, PengBerglund, Andreas34
____________Wu, PengHarrington, Mark34
____________Wu, PengAlexander, Sean34
________Abolrous, HazemArifin, Zainal23
____________Arifin, ZainalKharatishvili, Tengiz34
____________Arifin, ZainalChai, Sean34
____________Arifin, ZainalBerge, Karen34
____________Arifin, ZainalNorred, Chris34
____Hamilton, JamesWright, A. Scott12
________Wright, A. ScottUddin, Sairaj23
________Wright, A. ScottVong, William23
________Wright, A. ScottBrewer, Alan23
________Wright, A. ScottLaMee, Brian23
____Hamilton, JamesAltman, Gary12
________Altman, GaryKleinerman, Christian23
____________Kleinerman, ChristianPenor, Lori34
____________Kleinerman, ChristianMacrae, Stuart34
____________Kleinerman, ChristianBerry, Jo34
____________Kleinerman, ChristianColeman, Pat34
________Altman, GaryHedlund, Magnus23
Sánchez, KenNorman, Laura01
____Norman, LauraBarreto de Mattos, Paula12
________Barreto de Mattos, PaulaCulbertson, Grant23
________Barreto de Mattos, PaulaChen, Hao23
________Barreto de Mattos, PaulaLuthra, Vidur23
________Barreto de Mattos, PaulaMartin, Mindy23
________Barreto de Mattos, PaulaJohnson, Willis23
____Norman, LauraLiu, David12
________Liu, DavidPoe, Deborah23
________Liu, DavidSpoon, Candy23
________Liu, DavidWalton, Bryan23
________Liu, DavidMoreland, Barbara23
________Liu, DavidTomic, Dragan23
________Liu, DavidSheperdigian, Janet23
________Liu, DavidSeamans, Mike23
____Norman, LauraKahn, Wendy12
________Kahn, WendyWord, Sheela23
____________Word, SheelaSandberg, Mikael34
____________Word, SheelaRao, Arvind34
____________Word, SheelaMeisner, Linda34
____________Word, SheelaOgisu, Fukiko34
____________Word, SheelaHee, Gordon34
____________Word, SheelaPellow, Frank34
____________Word, SheelaKurjan, Eric34
____________Word, SheelaHagens, Erin34
____________Word, SheelaMiller, Ben34
____________Word, SheelaHill, Annette34
____________Word, SheelaHillmann, Reinout34
____Norman, LauraBarber, David12
Sánchez, KenTrenary, Jean01
____Trenary, JeanConroy, Stephanie12
________Conroy, StephanieSharma, Ashvini23
________Conroy, StephanieConnelly, Peter23
____Trenary, JeanBerg, Karen12
____Trenary, JeanMeyyappan, Ramesh12
____Trenary, JeanBacon, Dan12
____Trenary, JeanAjenstat, François12
____Trenary, JeanWilson, Dan12
____Trenary, JeanBueno, Janaina12
Sánchez, KenWelcker, Brian01
____Welcker, BrianJiang, Stephen12
________Jiang, StephenBlythe, Michael23
________Jiang, StephenMitchell, Linda23
________Jiang, StephenCarson, Jillian23
________Jiang, StephenVargas, Garrett23
________Jiang, StephenReiter, Tsvi23
________Jiang, StephenAnsman-Wolfe, Pamela23
________Jiang, StephenIto, Shu23
________Jiang, StephenSaraiva, José23
________Jiang, StephenCampbell, David23
________Jiang, StephenMensa-Annan, Tete23
____Welcker, BrianAbbas, Syed12
________Abbas, SyedTsoflias, Lynn23
____Welcker, BrianAlberts, Amy12
________Alberts, AmyValdez, Rachel23
________Alberts, AmyPak, Jae23
________Alberts, AmyVarkey Chudukatil, Ranjit23

No comments:

Post a Comment