Solved

sql server 2008 query

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

12 Experts available now in Live!

Get 1:1 Help Now