sql - Trying to get a column value of an ancestor when using HIERARCHYID -
i'm evaluating hierarchyid data type see if it'll meet needs project. hoping use manager of given employee id. i'm looking @ tables in adventureworks db (the 2012 version), person.person , humanresources.employee tables.
my query goes this:
select pp.lastname + ', ' + pp.firstname name, cast(hre.nationalidnumber bigint) id, hre.loginid, hre.organizationnode.getancestor(1) managerid, hre.jobtitle, hre.birthdate, hre.maritalstatus, hre.gender, hre.hiredate person.person pp inner join humanresources.employee hre on pp.businessentityid = hre.businessentityid
now expected because getancestor() clr function i'd have been able hre.organizationnode.getancestor(1).nationalidnumber managerid told i'm not worthy fast.
what i'm doing wrong here?
edit later on
playing around leads me hardly optimal (or it?)
select pp.lastname + ', ' + pp.firstname name, cast(hre.nationalidnumber bigint) id, hre.loginid, null managerid, hre.jobtitle, hre.birthdate, hre.maritalstatus, hre.gender, hre.hiredate, hre.organizationlevel person.person pp inner join humanresources.employee hre on pp.businessentityid = hre.businessentityid organizationlevel = 0 union select pp.lastname + ', ' + pp.firstname name, cast(hre.nationalidnumber bigint) id, hre.loginid, cast(hre2.nationalidnumber bigint) managerid, hre.jobtitle, hre.birthdate, hre.maritalstatus, hre.gender, hre.hiredate, hre.organizationlevel person.person pp inner join humanresources.employee hre on pp.businessentityid = hre.businessentityid inner join humanresources.employee hre2 on hre.organizationnode.getancestor(1) = hre2.organizationnode order hre.organizationlevel asc
a correlated sub-query seems me:
select pp.lastname + ', ' + pp.firstname name, cast(hre.nationalidnumber bigint) id, hre.loginid, ( select nationalidnumber humanresources.employee m organizationnode = hre.organizationnode.getancestor(1) )as managerid, hre.jobtitle, hre.birthdate, hre.maritalstatus, hre.gender, hre.hiredate person.person pp inner join humanresources.employee hre on pp.businessentityid = hre.businessentityid
Comments
Post a Comment