-1

我有数据表说

角色定位

RL1 CR1 LG1
RL2 CR2 LG2
RL3 CR3 LG3
RL4 CR4 LG4
RL5 CR5 LG1
RL6 CR6 LG1

契约角色

CR1 Policy1
CR2 Policy1
CR3 Policy1
CR4 Policy1
CR5 Policy2
CR6 Policy2

我只想取

RL2
RL3
RL4

所以我在下面写的查询工作正常,但我怀疑它会表现良好..任何帮助表示赞赏。谢谢

SELECT 
    RL.ROLE_LOCATION_ID 
FROM 
    ROLELOCATION RL, contractrole CR
WHERE 
    CR.CONTRACT_ROLE_ID = RL.CONTRACT_ROLE_ID
    AND CR.CONTR_COMPONENT_ID = policy1
    AND RL.LOCATION_GROUP_ID NOT IN (SELECT RL2.LOCATION_GROUP_ID 
                                     FROM ROLELOCATION RL2,CONTRACTROLE CR2 
                                     WHERE RL2.CONTRACT_ROLE_ID = CR2.CONTRACT_ROLE_ID 
                                     AND CR2.CONTR_COMPONENT_ID <> policy1)
4

1 回答 1

0

这是进行查询的方法:

select role_location_id
from (SELECT RL.ROLE_LOCATION_ID,
             sum(case when cr.policy <> 'policy1' then 1 else 0 end) over (partition by rl.location_group_id) as NumNotPolicy1
      FROM ROLELOCATION RL join
           contractrole CR
           on CR.CONTRACT_ROLE_ID = RL.CONTRACT_ROLE_ID
     ) t
where NumNotPolicy1 = 0

这会计算非策略 1 在位置组级别出现的次数。它只选择此计数为 0 的行。

于 2013-03-27T15:26:04.457 回答