Solved

sql server 2008 query

Posted on 2014-12-03
1
246 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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