SQL查询从表中查找报告人名称 [英] SQL Query to find Reportee name From table

查看:107
本文介绍了SQL查询从表中查找报告人名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Employee的表,其样本数据如下:


EmpID EmpName ReporteeToEmpID EmpCode
1 Ram 3 ABC111
2 Shyam 3 ABC112
table>
3 karan 4 ABC113

4 Alex 4 ABC114


这里的"ReporteeToEmpID"列指的是Emp向其报告的"EmpID".例如Ram的"ReporteeToEmpId"为3,即Karan EmpID,因此Ram正在向Karan报告.

我需要的结果是:

EmpCode EmpName ReportingToEmployeeName
ABC111 Ram Karan
ABC112 Shyam karan
ABC113 Karan Alex
ABC114 Alex Alex

I have a table called Employee with sample Data as follow:


EmpIDEmpNameReporteeToEmpIDEmpCode
1Ram3ABC111
2Shyam3ABC112
3karan4ABC113

4Alex4ABC114


Here the Column "ReporteeToEmpID" is referring the "EmpID" to whom the Emp is Reporting to.For example Ram''s "ReporteeToEmpId" is 3 i.e. Karan EmpID so Ram is Reporting to Karan.

I need the Result as :

EmpCodeEmpNameReportingToEmployeeName
ABC111RamKaran
ABC112Shyamkaran
ABC113KaranAlex
ABC114AlexAlex

推荐答案

尝试此查询..

try this query..

select A.EmpCode, A.EmpName, B.EmpName as 'Reporting To Employee' from EmpTable as A inner join EmpTable as B on A.ReporteeToEmpID=B.EmpID



希望它能工作..



hope it works..


这篇关于SQL查询从表中查找报告人名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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