Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ChumChum
Starting Member
3 Posts |
Posted - 2014-03-13 : 06:25:06
|
Hi, I am new to SQL and kindly need help with the followingI need to output the following view_______________________________________ |Name |Surname|Position|ManagerName | --------------------------------------- |Donald |Duck |Support |Scrooge |---------------------------------------|Scrooge|McDuck |Manager |Daizy |---------------------------------------Below are the tablestable1 id|Name |Surname|Position| ---------------------------- 1 |Scrooge|McDuck |Manager | ---------------------------- 2 |Daizy |Duck |Manager | ---------------------------- 3 |Donald |Duck |Support | ---------------------------- Table2|EmpID|ManID||-----------|| 3 | 1 || 1 | 2 |------------- This is my code but it does not output the correct manager nameSELECT * FROM(SELECT NAME, SURNAME, POSITION FROM EMPLOYEE, STAFFLINK WHERE ID = EMPID),(SELECT NAME AS MANAGERNAME, POSITION AS MANAGERPOSITION FROM EMPLOYEE, STAFFLINK WHERE ID = MANID) |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-13 : 07:33:25
|
Check if this works for youSELECT Table1.Name,Table1.SurName,table1.Position,Manager.Name as ManagerNameFROM Table1INNER JOIN Table2 ON table1.ID=table2.EmpIDINNER JOIN Table1 Manager ON Maanager.ID=Table2.ManIDCheersMIK |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-03-13 : 07:34:52
|
DECLARE @table1 TABLE(id INT,Name VARCHAR(20),Surname VARCHAR(20),Position VARCHAR(20))INSERT INTO @table1 VALUES(1,'Scrooge','McDuck','Manager'),(2,'Daizy','Duck','Manager'),(3,'Donald','Duck','Support')DECLARE @Table2 TABLE(EmpID INT,ManID INT)INSERT INTO @Table2 VALUES(3,1),(1,2)select t.Name,t.Surname,t.Position,ttt.Name as 'ManagerName'from @table1 as tinner join @Table2 as tton t.id = tt.EmpIDinner join @table1 as ttton ttt.id = tt.manidVeera |
|
|
ChumChum
Starting Member
3 Posts |
Posted - 2014-03-13 : 08:13:56
|
Thanks for the reply Mik and Veera. You guys are super awesome it works. I need to master inner join statements.Thanks Guys |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2014-03-13 : 08:31:14
|
WelcomeVeera |
|
|
ChumChum
Starting Member
3 Posts |
Posted - 2014-03-13 : 10:51:56
|
I am trying to teach myself SQL but its not going so good.I need help again using the above tables, how do i get these outputs|Name |Surname|Position|ManagerName |---------------------------------------|Scrooge |McDuck |Manager |Daizy |---------------------------------------|Daizy | Duck |Manager |Scrooge |--------------------------------------|Donald | Duck |Support |Scrooge |-And how do i get this output|StaffCount | Name |--------------------------------------| 4 | Daizy |--------------------------------------| 3 | Scrooge | |
|
|
|
|
|
|
|