SQL 查询逻辑以获取所有可能层次结构的 IsActive 为 false 的 ID
SQL query logic to get Ids which have IsActive false for all the possible hierarchies
最终结果不应包括 id 24,因为此 id 与公司层次结构具有活动映射:1176->1781->1787 并且 1787 在第二个 table 中处于活动状态。
我尝试的查询无法得到想要的结果。
示例数据:
Table 1 #tempCTE_CompMCompDir:
Id CompanyId ChildId
24 1776 1781
24 1776 1782
25 1777 1783
24 1781 1786
24 1781 1787
Table 2 #tempContComM:
CompanyId ContactId IsActive
1787 2903 1
1783 2903 0
1778 2903 0
1786 2903 0
输入:
Table 1 家有分层数据公司和 children.
Table 2 家拥有 IsActive true 或 false 的叶级公司。
输出:
- 从 Table 1
中获取所有 ID
如果来自 table 1 的公司 ID 或 Child Id 通过层次结构映射到 table 2 的公司 ID,并且此映射的 IsActive 在所有情况下都是错误的。
示例数据的输出应为:
Id
25
我的错误输出查询是:
SELECT CTE_CompMCompDir.Id
FROM #tempCTE_CompMCompDir CTE_CompMCompDir
JOIN #tempContComM tempContComM
ON ( CTE_CompMCompDir.CompanyId = tempContComM.CompanyId
AND tempContComM.IsActive = 0)
OR ( CTE_CompMCompDir.ChildId = tempContComM.CompanyId
AND tempContComM.IsActive = 0)
如果您只查找第二个 table 中的所有子行条目的可能解决方案,这些条目具有所有假 Isactive 值
create table tempCTE_CompMCompDir (Id int, CompanyId int, ChildId int)
insert into tempCTE_CompMCompDir
values
(24, 1776, 1781),
(24, 1776, 1782),
(25, 1777, 1783),
(24, 1781, 1786),
(24, 1781, 1787);
create table tempContComM(CompanyId int, ContactId int, IsActive bit);
insert into tempContComM
values
(1787 ,2903 , 1),
(1783 ,2903 , 0),
(1778 ,2903 , 0),
(1786 ,2903 , 0);
select t.* from tempCTE_CompMCompDir t join (
select
id,value=max(case when ISNULL(Isactive,-1)=0 then 0 else 1 end)
from tempCTE_CompMCompDir l
left join tempContComM r
on l.companyid=r.companyid or l.childid=r.companyid
group by id
) t2
on t.id=t2.id and t2.value=0
一些注意事项:
- 我将缺失的映射值视为既非假也非真
- 您可以调整 max (case..) 来更改您的规则和标准
此查询将 CompanyId 或 ChildId 与 raw where IsActive = 1 匹配的所有 #tempCTE_CompMCompDir.Id 丢弃到第二个 table;这是我对你想要达到的目标的理解
select T1.Id
from #tempCTE_CompMCompDir T1
inner join #tempContComM T2
on T1.CompanyId = T2.CompanyId or T1.ChildId = T2.CompanyId
group by T1.Id
having max(cast(IsActive as int)) = 0
最终结果不应包括 id 24,因为此 id 与公司层次结构具有活动映射:1176->1781->1787 并且 1787 在第二个 table 中处于活动状态。 我尝试的查询无法得到想要的结果。
示例数据:
Table 1 #tempCTE_CompMCompDir:
Id CompanyId ChildId
24 1776 1781
24 1776 1782
25 1777 1783
24 1781 1786
24 1781 1787
Table 2 #tempContComM:
CompanyId ContactId IsActive
1787 2903 1
1783 2903 0
1778 2903 0
1786 2903 0
输入:
Table 1 家有分层数据公司和 children.
Table 2 家拥有 IsActive true 或 false 的叶级公司。
输出:
- 从 Table 1 中获取所有 ID
如果来自 table 1 的公司 ID 或 Child Id 通过层次结构映射到 table 2 的公司 ID,并且此映射的 IsActive 在所有情况下都是错误的。
示例数据的输出应为:
Id
25
我的错误输出查询是:
SELECT CTE_CompMCompDir.Id
FROM #tempCTE_CompMCompDir CTE_CompMCompDir
JOIN #tempContComM tempContComM
ON ( CTE_CompMCompDir.CompanyId = tempContComM.CompanyId
AND tempContComM.IsActive = 0)
OR ( CTE_CompMCompDir.ChildId = tempContComM.CompanyId
AND tempContComM.IsActive = 0)
如果您只查找第二个 table 中的所有子行条目的可能解决方案,这些条目具有所有假 Isactive 值
create table tempCTE_CompMCompDir (Id int, CompanyId int, ChildId int)
insert into tempCTE_CompMCompDir
values
(24, 1776, 1781),
(24, 1776, 1782),
(25, 1777, 1783),
(24, 1781, 1786),
(24, 1781, 1787);
create table tempContComM(CompanyId int, ContactId int, IsActive bit);
insert into tempContComM
values
(1787 ,2903 , 1),
(1783 ,2903 , 0),
(1778 ,2903 , 0),
(1786 ,2903 , 0);
select t.* from tempCTE_CompMCompDir t join (
select
id,value=max(case when ISNULL(Isactive,-1)=0 then 0 else 1 end)
from tempCTE_CompMCompDir l
left join tempContComM r
on l.companyid=r.companyid or l.childid=r.companyid
group by id
) t2
on t.id=t2.id and t2.value=0
一些注意事项:
- 我将缺失的映射值视为既非假也非真
- 您可以调整 max (case..) 来更改您的规则和标准
此查询将 CompanyId 或 ChildId 与 raw where IsActive = 1 匹配的所有 #tempCTE_CompMCompDir.Id 丢弃到第二个 table;这是我对你想要达到的目标的理解
select T1.Id
from #tempCTE_CompMCompDir T1
inner join #tempContComM T2
on T1.CompanyId = T2.CompanyId or T1.ChildId = T2.CompanyId
group by T1.Id
having max(cast(IsActive as int)) = 0