SQL

时间:2015-08-18 04:04:42

标签: sql sql-server-2014 recursive-query

我的分层表名dept_employees的结构如下:

--------------------------------------
| id | parent_id | obj_id | obj_type |
--------------------------------------
| 1  |    null   |    34  |   dept   |
--------------------------------------
| 2  |       3   |    78  |   agent  |
--------------------------------------
| 3  |       1   |    78  | employee |
--------------------------------------
| 4  |       2   |   79   |   agent  |
--------------------------------------
| 5  |       3   |   80   |   agent  |
--------------------------------------
--------------------------------------
--------------------------------------
|1500|       2   |   934  | employee |
--------------------------------------
|1501|       4   |   935  | employee |
--------------------------------------

如果obj_type = 'agent'

我需要从另一个名为Agents的表中删除它,Agents看起来像:

--------------------------------------
| id |    name   |  phone |  Adress  |
--------------------------------------
| 1  |   Shynaz  |   123  |   Almaty |
--------------------------------------
| 2  |   Damir   |   589  |   Astana |
--------------------------------------
| 3  |   Oljas   |   637  | Karagand |
--------------------------------------
--------------------------------------
--------------------------------------
| 78 |   Erasyl  |   743  |   Aktau  |
--------------------------------------
| 79 |   Erok    |   743  |   Atyrau |
--------------------------------------

或者如果obj_type = 'employee',那么我需要从另一个名为Employees的表中获取它。员工看起来像:

--------------------------------------
| id |    name   |  office |  salary |
--------------------------------------
| 1  |   Asyl    |   123  |   1000$  |
--------------------------------------
| 2  |   Zhandos |   589  |   1500$  |
--------------------------------------
--------------------------------------
--------------------------------------
| 78 |   Aleksei |   637  |    500$  |
--------------------------------------
--------------------------------------
--------------------------------------
|934|   Alibi   |   7    |    980$  |
--------------------------------------
|935|   Azat    |   8    |    980$  |
--------------------------------------

在第一个表parent_idid在同一个表中,但在另一个记录中。 例如:我得到agents的所有记录的employeersid = 2。 我需要输出如:

------------
id | name  |
------------
79 | Erok  |
------------
934| Alibi |
------------

我的疑问是:

WITH ChildNodes(id, name) 
AS(
SELECT 
   parent_de.id, 
   case parent_de.obj_type
    When 'agent' then parent_l.Name
    When 'employee' then parent_e.Fullname
   end as name
FROM FstP_Restored.dbo.dept_employees parent_de
left Join FstP_Restored.dbo.employees parent_e
    On parent_de.obj_id = parent_e.id --and parent_de.obj_type = 'employee'
left Join Agents_Restored.dbo.Лица parent_l 
    On parent_de.obj_id = parent_l.ID --and parent_de.obj_type = 'agent'
WHERE parent_de.id = 2--and parent_de.obj_type = isnull('agent', 'employees')

UNION ALL
SELECT
   child_de.id, 
   case child_de.obj_type
    When 'agent' then child_l.Name
    When 'employee' then child_e.Fullname
   end as name
FROM FstP_Restored.dbo.dept_employees child_de
 Inner Join FstP_Restored.dbo.employees child_e 
    On child_de.obj_id = child_e.id --and child_de.obj_type = 'employee' 
 Inner Join Agents_Restored.dbo.Лица child_l
    On child_de.obj_id = child_l.ID --and child_de.obj_type = 'agent'
 Inner JOIN ChildNodes parent ON parent.id = child_de.parent_id)

SELECT 
ChildNodes.id, 
ChildNodes.name
FROM ChildNodes
Where ChildNodes.id!= 2

有了这个查询,我什么也没得到。我做错了什么? SQL Server 2014

1 个答案:

答案 0 :(得分:2)

未经测试,但似乎是一件简单的事情,不是吗?可以扩展为更多对象类型。

SELECT
    t1.obj_id AS id,
    CASE t1.obj_type
        WHEN 'agent' THEN t2.name
        WHEN 'employee' THEN t3.name
        ELSE 'unknown'
    AS name
FROM dept_employees t1
LEFT JOIN Agents t2 ON t1.obj_id=t2.id
LEFT JOIN Employees t3 ON t1.obj_id=t3.id
WHERE t1.parent_id=2