Author |
Topic |
Natalia89
Starting Member
12 Posts |
Posted - 2013-12-04 : 16:27:05
|
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 EmployeeGOCREATE 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
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-12-04 : 16:40:51
|
select e.emp_id, e.name, s.name as supervisorfrom employee eleft outer join employee s on s.emp_id = e.supervisorBe One with the OptimizerTG |
|
|
Natalia89
Starting Member
12 Posts |
Posted - 2013-12-04 : 16:42:16
|
its not the join statement but thanks anywaysaddf |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-12-04 : 16:54:30
|
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.namefrom ( select supervisor from employee group by supervisor ) sjoin employee e on e.emp_id = s.supervisor Be One with the OptimizerTG |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-12-05 : 02:00:21
|
[code]select e.emp_id, e.namefrom employee ewhere exists (select 1 from employee where supervisor = e.emp_id)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Natalia89
Starting Member
12 Posts |
Posted - 2013-12-05 : 15:32:57
|
Thanks it worked.see if i wanted to display emp_id without the e only the numbers how do i do that?saddf |
|
|
Natalia89
Starting Member
12 Posts |
Posted - 2013-12-05 : 15:33:23
|
what i meant was how do i only display a part of data from the columnlets say emp_id is E3456 and all i want to display is 3456 without that E? any ideas?saddf |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-12-05 : 16:25:03
|
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 OptimizerTG |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-12-05 : 16:53:04
|
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=190118#742347please stop cross posting (posting the same question in multiple threads).thanksBe One with the OptimizerTG |
|
|
|