我需要员工详细信息 [英] I need employee Details

查看:73
本文介绍了我需要员工详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此表中我有员工详细信息。



EmpId EmpName ReportID

101 User1 201

102 User2 401

201 User3 301

301 User4 null



如果我选择employeeID 101,我需要获取employeeid 101的报告ID谁是employeeid 201的报告ID,谁是employeeid 301的报告ID



我需要结果应该是:



101 User1 201

201 User3 301

301 User4 null

解决方案

尝试这样的事情...

 创建  #Employee 

EmpId Int
EmpName Nvarchar 40 ),
ReportID Int


Ins ert into #Employee
101 ' User1' 201 ),( 102 ' User2 ' 401 ),( 201 ' User3' 301 ),( 301 ' User4' null

; cte as

选择 EmpId,EmpName,ReportID 来自 #Employee 其中 EmpId = 101 - - 在此处提供EmpId
联盟 全部
选择 e.EmpId,e.EmpName,e.ReportID
来自 #Employee e
内部 join cte c on c.ReportID = e.EmpId


选择 * From cte

Drop #员工



输出:

 EmpId EmpName ReportID 
101 User1 201
201 User3 301
301 User4 NULL


从TableName中选择*其中EmpId<> 102 


In this table i have a employee details.

EmpId EmpName ReportID
101 User1 201
102 User2 401
201 User3 301
301 User4 null

If I select the employeeID 101,i need to get who is the report id for the employeeid 101 and who is the report id for the employeeid 201 and who is the report id for the employeeid 301

I need the result should be like:

101 User1 201
201 User3 301
301 User4 null

解决方案

try Some thing like this...

Create Table #Employee
	(
	 EmpId Int,
	 EmpName Nvarchar(40),
	 ReportID Int
	)

Insert into #Employee
Values(101,'User1',201),(102,'User2',401),(201,'User3',301),(301,'User4',null)

;with cte as
 ( 
   Select EmpId,EmpName,ReportID From #Employee Where EmpId =101-- Give EmpId Here
   Union all
   Select e.EmpId,e.EmpName,e.ReportID
   From #Employee e 
   Inner join cte c on c.ReportID = e.EmpId 
 )

 Select * From cte

Drop Table #Employee


Output:

EmpId	EmpName	ReportID
101	User1	201
201	User3	301
301	User4	NULL


Select * From TableName Where EmpId <> 102


这篇关于我需要员工详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆