SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 sql
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Natalia89
Starting Member

Poland
12 Posts

Posted - 12/04/2013 :  16:27:05  Show Profile  Reply with Quote
I have a table that contains empid, name, salary, hiredate, positionand supervisor (which includes empid not the name) how do i list the empid and name of all supervisors ? the output has to have to columns supervisor( and a list of their emid) and their names. Thats the create statement used to create the employee table:
/* Create table Employee */
IF OBJECT_ID('Employee', 'U') IS NOT NULL
DROP TABLE Employee
GO
CREATE TABLE Employee (
emp_id NCHAR(5),
name NVARCHAR(20),
position NVARCHAR(20),
hire_date DATETIME,
salary MONEY,
bcode NCHAR(3),
supervisor NCHAR(5)
)

i have tried a variety of statements using having statement and count but the dont seem to work:/

select emp_id, name from employee where position='manager'; i tried this but it doesnt work anoune smart that knows how to do it?


saddf

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 12/04/2013 :  16:40:51  Show Profile  Reply with Quote
select e.emp_id, e.name, s.name as supervisor
from employee e
left outer join employee s on s.emp_id = e.supervisor

Be One with the Optimizer
TG
Go to Top of Page

Natalia89
Starting Member

Poland
12 Posts

Posted - 12/04/2013 :  16:42:16  Show Profile  Reply with Quote
its not the join statement but thanks anyway


saddf
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 12/04/2013 :  16:54:30  Show Profile  Reply with Quote
sorry, I misread your question. So just all supervisors, right? you don't need who they are supervising? Try this:

select e.emp_id, e.name
from   (
       select supervisor 
       from employee 
       group by supervisor
       ) s
join   employee e on e.emp_id = s.supervisor


Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/05/2013 :  02:00:21  Show Profile  Reply with Quote

select e.emp_id, e.name
from   employee e
where exists (select 1 from employee where supervisor = e.emp_id)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

Natalia89
Starting Member

Poland
12 Posts

Posted - 12/05/2013 :  15:32:57  Show Profile  Reply with Quote
Thanks it worked.
see if i wanted to display emp_id without the e only the numbers how do i do that?


saddf
Go to Top of Page

Natalia89
Starting Member

Poland
12 Posts

Posted - 12/05/2013 :  15:33:23  Show Profile  Reply with Quote
what i meant was how do i only display a part of data from the column
lets say emp_id is E3456 and all i want to display is 3456 without that E? any ideas?

saddf
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 12/05/2013 :  16:25:03  Show Profile  Reply with Quote
check out String Functions in Books Online.

If it is always the first 1 character then you could use:
stuff(emp_id, 1, 1, '')

if it is always an 'E' anywhere then you could use:
replace(emp_id, 'E', '')

Look through the functions and see which will work best for your situation.


Be One with the Optimizer
TG
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 12/05/2013 :  16:53:04  Show Profile  Reply with Quote
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=190118#742347

please stop cross posting (posting the same question in multiple threads).
thanks

Be One with the Optimizer
TG
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000