Sql server 在SQL Server 2014中呈现不同条件的分层数据

Sql server 在SQL Server 2014中呈现不同条件的分层数据,sql-server,tsql,Sql Server,Tsql,我有两张桌子。在一个表中,我的原始数据和另一个表中的关系如下所示 Declare @Emp table(EmpId int,EmpName Varchar(100),CITY VARCHAR(100),Designation Varchar(100),ReportingManager Int) INSERT INTO @Emp VALUES(1,'Ram','Hyderabad','TL',6) ,(2,'Laxman','Hyderabad','TL',9) ,(3,'Suresh','Ban

我有两张桌子。在一个表中,我的原始数据和另一个表中的关系如下所示

Declare @Emp table(EmpId int,EmpName Varchar(100),CITY VARCHAR(100),Designation Varchar(100),ReportingManager Int)
INSERT INTO @Emp
VALUES(1,'Ram','Hyderabad','TL',6)
,(2,'Laxman','Hyderabad','TL',9)
,(3,'Suresh','Bangalore','Officer',6)
,(4,'Rajesh','Bangalore','Officer',9)
,(5,'Lokesh','Delhi','TL',6)
,(6,'Venkatesh','Mumbai','Manager',6)
,(7,'Subbu','Patna','Officer',9)
,(8,'Ravi','Hyderabad','Officer',9)
,(9,'Sai','Hyderabad','Manager',9)
,(10,'Satish','Hyderabad','Officer',6)

DECLARE @EmpRelation TABLE(EmpRelationShipID INT IDENTITY NOT NULL,ReportingTo INT,EmpID INT)
INSERT INTO @EmpRelation
VALUES(1,6)
,(2,9)
,(3,1)
,(4,5)
,(5,6)
,(7,2)
,(8,5)
,(10,1)
  • 这里,@Emp表中的ReportingManager列指出,如果Emp报告TL,那么TL的ManagerName
  • 此处,@EmpRelation中的ReportingTo列表示他向谁报告。(TL或经理)
  • 官员向TLs报告,TLs向经理报告
我想要下面的数据

[EMPID],[EMPNAME],[CITY],[IsManager],[HasSubordinates],[IsSubordinate],[ManagerCity],[SubordinatesList] 
  • 此处的[IsManager]字段是,如果员工是经理,则为“是”,否则为“否”。在@Emp表中,“Sai”是经理,所以对他来说这是肯定的

  • [HasSubordinates]如果员工手下有员工,则“是”或“否”。在这种情况下,对于TLs和管理者,此列为是

  • [IsSubordinate]是指如果员工向任何人报告,则在本例中,TL是,而管理人员是

  • [ManagerCity]假设一名员工向TL报告,那么他的TLs经理将负责该城市

  • [下属列表]在TL/经理手下工作的员工。对于军官来说,它是空白的


我无法获取正确的数据。任何人都可以研究一下这个问题,并向我提出这个问题。

下面的例子应该会给你一些想法。它不会遍历层次结构,即使用递归CTE,因为似乎没有依赖于在层次结构上下检查多个级别的结果

select E.EmpId, E.EmpName, E.City, E.Designation,
  -- Anyone who reports to themselves is a manager.
  case when E.EmpId = E.ReportingManager then 'Yes' else 'No' end as IsManager,
  -- Anyone with a related employee reporting to them has subordinates.
  case when exists ( select 42 from @EmpRelation as iER where iER.ReportingTo = E.EmpId )
    then 'Yes' else 'No' end as HasSubordinates,
  -- Anyone who reports to an employee is a subordinate.  (Should this exclude reporting to themselves?)
  case when exists ( select 42 from @EmpRelation as iER where iER.EmpId = E.EmpId )
    then 'Yes' else 'No' end as IsSubordinate,
  -- The city of the reporting manager, if any.
  ME.City as ManagerCity,
  -- A comma-delimited list of employees who report directly to the current employee.
  --   Modern versions of SQL Server could use   String_Agg .
  Stuff( (
    select ',' + sE.EmpName
      from @Emp as sE inner join @EmpRelation as sER on sER.EmpId = se.EmpId
      where sER.ReportingTo = E.EmpId
      order by sE.EmpName for XML path(''), type).value('.[1]', 'VarChar(max)' ),
    1, 1, '' ) as SubordinatesList
  from @Emp as E left outer join
    @Emp as ME on ME.EmpId = E.ReportingManager;
感谢您提供可用的样本数据


根据OP的评论,
@EmpRelation
表有点令人困惑
@EmpRelation
具有
(3,1)
(10,1)
作为
(ReportingTo,EmpId)
。这并不意味着
EmpId
1
报告到
3
10
(也称为矩阵管理)。修改查询以翻转关系需要对
HasSubordinates
IsSubordinate
下属列表进行细微更改:

select E.EmpId, E.EmpName, E.City, E.Designation,
  -- Anyone who reports to themselves is a manager.
  case when E.EmpId = E.ReportingManager then 'Yes' else 'No' end as IsManager,
  -- Anyone with a related employee reporting to them has subordinates.
  case when exists ( select 42 from @EmpRelation as iER where iER.EmpId = E.EmpId )
    then 'Yes' else 'No' end as HasSubordinates,
  -- Anyone who reports to an employee is a subordinate.  (Should this exclude reporting to themselves?)
  case when exists ( select 42 from @EmpRelation as iER where iER.ReportingTo = E.EmpId )
    then 'Yes' else 'No' end as IsSubordinate,
  -- The city of the reporting manager, if any.
  ME.City as ManagerCity,
  -- A comma-delimited list of employees who report directly to the current employee.
  --   Modern versions of SQL Server could use   String_Agg .
  Stuff( (
    select ',' + sE.EmpName
      from @Emp as sE inner join @EmpRelation as sER on sER.ReportingTo = se.EmpId
      where sER.EmpId = E.EmpId
      order by sE.EmpName for XML path(''), type).value('.[1]', 'VarChar(max)' ),
    1, 1, '' ) as SubordinatesList
  from @Emp as E left outer join
    @Emp as ME on ME.EmpId = E.ReportingManager;

您好,非常感谢您的努力和回复。这里的字段[HasSubordinates]给出了错误的结果。本栏为“如果员工有任何下属,则为“是”。在本栏中,只有TL和经理有“是”。但EmpID 3对他来说是“官员”,这应该是“否”。请检查一下。[下属列表]colmn也错了。本栏表示在他手下工作的员工。在本例中,在员工“Ram”下“如果我们看到@EmpRelation表(员工3和10在EmployeeID 1下工作),两名员工正在工作@sasibhushan-我更新了答案,添加了一个带有修改的
where
子句的查询,以翻转从属关系。非常感谢您