MS SQL
I'm attempting to look up the name of the vp that everyone reports up to. I believe I need to use a CTE, however, I'm struggling to put the query together. Any assistance or guidance would be greatly appreciated.
Sample table
CREATE TABLE #Temp1
(
UserName varchar(30),
UserTitle varchar(30),
ManagerName varchar(30)
);
INSERT INTO #Temp1
VALUES
('dave','CEO',null),
('bill','vp','dave'),
('mike','vp','dave'),
('dave','vp','mike'),
('linda','cashier','mike'),
('sherry','manager','mike'),
('dawn','assistant manager','bill'),
('tom','electrician','sherry'),
('todd','security guard','dawn');
The sql query output would contain UserName in one column and the name of vp they report up to, or null if no vp above them, in the next column. For example
dave, null
bill, null
mike, null
dave, mike
linda, mike
sherry, mike
dawn, bill
tom, mike
todd, bill