Solved

sql server 2008 query

Posted on 2014-12-03
1
244 Views
Last Modified: 2014-12-03
I'm using SQL Server 2008 R2.

I have a table that looks like this:

table
Here is my fiddle for it: http://sqlfiddle.com/#!3/9ad11/2

If you look at the table. The value in the BossID column contains the EmployeeID of that employee's boss.
So if you look at my table Homer, Michael, Taylor and Erin's BossID is 2451 which pertains to the EmployeeID of value 2451 which is Montgomery Burns.  
So Montgomery Burns is Homer, Michael, Taylor and Erin's Boss.
Now if you look at the BossID value of 2356 for Montgomery Burns that pertains to EmployeeID of value 2356. So Donal Trump is Montgomery Burn's boss.

What I want is to write a query that takes in one parameter called @username.
So when I feed it the username of an employee the result set is all of the people that are his employees.
So for example if I set the parameter like this SET @UserName = 'mburns' then the result set should show all the employees which work for montgomery burns. Which in the case of my table would look like this:

result set when parameter is set to 'mburns'
if I set the parameter like this SET @UserName = 'dtrump' then the result set should show all the employees which work for donald trump. Which in the case of my table would look like this:

result set when parameter is set to dtrump

Right now I have the following query:

DECLARE @UserName varchar(50)
SET @UserName = 'mburns'

SELECT [Rid]
      ,[Lname]
      ,[Fname]
      ,[UserName]
      ,[Region]
      ,[OrderYear]
      ,[BossID]
      ,[EmployeeID]
FROM [TestR1]
WHERE [UserName] = @UserName

But that gives me this:

Here is the fiddle for my query:  http://sqlfiddle.com/#!3//3

result set of my query

Any idea of how I need to revise my query?
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 3

Accepted Solution

by:
Richard Obenchain earned 500 total points
ID: 40478789
DECLARE @UserName varchar(50)
 SET @UserName = 'mburns'

SELECT t1.[Rid]
       ,t1.[Lname]
       ,t1.[Fname]
       ,t1.[UserName]
       ,t1.[Region]
       ,t1.[OrderYear]
       ,t1.[BossID]
       ,t1.[EmployeeID]
 FROM [TestR1] t1 join TestR1 t2 on t1.bossid = t2.employeeid
 WHERE t2.[UserName] = @UserName
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now