У меня есть таблица с именем wws_fa
Я хочу найти что-то вроде этого
пока я не нашел верхний уровень
как мне это сделать с помощью SQL
но это перейдет на один уровень выше только когда vo_refid != 0
Я хочу добавить CTE или любое решение, чтобы я мог перенести fa_fatherid в View
| fa_refid | fa_id | vo_refid | fa_typ |
| -------- | -------- | -------- | -------- |
| 12 | V08015 | 0 | 37 |
| 1790 | V42413 | 12 | 25 |
| 2852 | V74736 | 1790 | 1004 |
| 7326 | V00880 | 6834 | 1 |
| fa_id | fa_fatherid |
| -------- | ----------- |
| V74736 | V08015 |
|V00880 | 211336 |
but I have some problemsI need to check if (the vo_refid = 0 or is null (fa_fatherid = fa_id))else if (fa_typ = 1 (fa_fatherid = select from another table a value))else go up in the search where ( new.fa_refid = vo_refid)
with fa_id_hierarchy (child_refid, father_id, vo_refid, fa_typ)
as
(
select fa.fa_refid as child_refid,
Case
when fa.vo_refid = 0 or fa.vo_refid is null then fa.fa_id
WHEN fa.vo_refid <> 0 AND fa.fa_typ = 1 THEN (SELECT auid FROM Deleco.dbo.au WHERE Deleco.dbo.au.aurefid = fa.fa_refid)
ELSE NULL
END AS father_id,
fa.vo_refid as vo_refid,
fa.fa_typ AS fa_typ
From DELECO.dbo.wws_fa AS fa
union all
select fa_child.child_refid as child_refid,
Case
when fa_father.vo_refid = 0 or fa_father.vo_refid is null then fa_father.fa_id
WHEN fa_father.vo_refid != 0 AND fa_father.fa_typ = 1 THEN (SELECT auid FROM Deleco.dbo.au WHERE Deleco.dbo.au.aurefid = fa_father.vo_refid)
ELSE NULL
END AS father_id,
fa_father.vo_refid as vo_refid,
fa_father.fa_typ as fa_typ
from fa_id_hierarchy as fa_child
join DELECO.dbo.wws_fa AS fa_father
on fa_child.vo_refid = fa_father.fa_refid
where fa_child.vo_refid IS NOT NULL and fa_child.vo_refid != 0
)
Надежда
Вопрос задан29 февраля 2024 г.