一个值应该是状态' ACTIVE'而其他所有人都应该被禁用'

时间:2017-12-26 07:37:51

标签: sql oracle oracle10g oracle9i

我想显示名称以a开头的用户名,状态应为' ACTIVE'对于特定的系统ID,除了所有状态应该被禁用'对于相同的系统ID。

下表用于显示记录: enter image description here

person_id,admin_person_id和username_id是这三个表的主键。

如果我在ADMIN_Person表中使用domain_id(LKJHC),其person_id为5678,则相应的人员ID的username_id就在那个用户名id中有很多系统id但是在这里我只想要状态&# 39; ACTIVE'对于system_id 123,用户名以(a-)开头,但除了所有123个system_id之外,其他所有系统都被禁用,除了用户名以& - ' ACTIVE'状态。

我使用的查询:

SELECT e.domain_id,e.username,e.system_id
FROM (SELECT p.domain_id,u.username, u.status, u.system_id 
     FROM username u  JOIN admin_person up ON u.username_id = 
     up.username_id JOIN person p ON up.person_id = p.person_id
     WHERE  u.  username LIKE 'a-%'
     AND u.status = 'ACTIVE'
     AND u.system_id = 123) E
     WHERE e.domain_id IN  ( SELECT p.domain_id
            FROM username u
            JOIN admin_person up ON u.username_id = up.username_id
            JOIN person p ON up.person_id = p.person_id
            WHERE u.status = 'DISABLED'
            AND  u.system_id = 123)

在这个结果中,我得到了123个活动用户名,没有用户名以-a开头。像(ADSF)

2 个答案:

答案 0 :(得分:0)

我已经使用了为每个(U.USERNAME NOT LIKE '%-a' AND U.STATUS = 'ACTIVE') OR (U.USERNAME LIKE '%-a' AND U.STATUS = 'DISABLED')计算违反规则DOMAIN_ID的实例的逻辑,并且仅当此计数为0时才会保留DOMAIN_ID

SELECT P1.DOMAIN_ID, U1.USERNAME, U1.SYSTEM_ID
FROM 
PERSON P1 INNER JOIN ADMIN_PERSON AP1 ON P1.PERSON_ID = AP1.PERSON_ID
INNER JOIN USERNAME U1 ON AP1.USERNAME_ID = U1.USERNAME_ID
INNER JOIN
    (SELECT P.DOMAIN_ID,
    SUM(CASE WHEN (U.USERNAME NOT LIKE '%a-' AND U.STATUS = 'ACTIVE') OR (U.USERNAME LIKE '%a-' AND U.STATUS = 'DISABLED') THEN 1 ELSE 0 END) AS NOT_ELIGIBLE
    FROM
    PERSON P INNER JOIN ADMIN_PERSON AP
    ON P.PERSON_ID = AP.PERSON_ID
    INNER JOIN USERNAME U
    ON AP.USERNAME_ID = U.USERNAME_ID
    GROUP BY P.DOMAIN_ID) A
ON P1.DOMAIN_ID = A.DOMAIN_ID
AND A.NOT_ELIGIBLE = 0
AND U1.USERNAME LIKE '%a-'; 

过滤DOMAIN_ID后,查询应选择USERNAME格式为a-

答案 1 :(得分:0)

分别获取countUsername not like 'a-%'的{​​{1}}值

嵌套内部SQL

并将它们均衡为SQL的外部

Status = 'DISABLED'

SQL Fiddle Demo