基于列的条件行

时间:2016-08-24 14:41:31

标签: sql oracle11g

在SQL中,如何根据列的值排除行。

E.g。我有一个名为PROJECT_MGR_CODE的列,其中值为General Manager 如果总经理存在于结果集中,那么我想取消总经理以上的董事和首席执行官。

如果总经理不存在于结果集中,那么它应该带有Director,如果Director不存在,那么它应该带CE

3 个答案:

答案 0 :(得分:2)

我正在为每一行分配层次结构级别,然后检查最高级别:

SELECT * FROM 
 (
   SELECT dt.*,
      -- max level over all rows
      MAX(hierarchy_level) OVER () AS max_level
   FROM
    (   
      SELECT T2.*,
         CASE
            WHEN project_mgr_code LIKE 'CE%'              THEN 1
            WHEN project_mgr_code LIKE 'Director%'        THEN 2
            WHEN project_mgr_code LIKE 'General Manager%' THEN 3
            ELSE NULL  -- all other rows
         END AS hierarchy_level
      FROM T2
    ) dt
 ) dt
WHERE hierarchy_level >= max_level -- best match manager
   OR hierarchy_level IS NULL      -- all other rows 

可以轻松增强此逻辑以添加更多级别。

答案 1 :(得分:1)

我想知道这是否符合您的要求:

select t2.*
from t2
where t2.project_mgr_code like 'General Manager%'
union all
select t2.*
from t2
where t2.project_mgr_code like 'Director%' and
      not exists (select 1 from t2 where t2.project_mgr_code like 'General Manager%');

这似乎就是你所描述的。

编辑:

对于常规优先级排序查询,请将row_number()order by中的信息一起使用:

select t2.*
from (select t2.*,
             row_number() over (order by (case when project_mgr_code like 'General Manager%' then 1
                                               when project_mgr_code like 'Director%' then 2
                                               when project_mgr_code like 'CE%' then 3
                                          end)
                               ) as seqnum
      from t2
     ) t2
where seqnum = 1;

答案 2 :(得分:1)

如果我正确理解了您的问题,那么您需要这样做:

QUERY:

SELECT *
FROM   T2
WHERE  project_mgr_code NOT LIKE 'Director%'
      AND project_mgr_code NOT LIKE 'CE%';

结果:

enter image description here

根据以下评论,这就是您所需要的:

“如果结果集中不存在总经理,那么它应该带有Director,如果Director不存在,那么它应该带CE”

QUERY:

SELECT *
FROM   #T2
WHERE  project_mgr_code LIKE CASE
                           WHEN
                              (SELECT COUNT(1)
                               FROM   #T2
                               WHERE  project_mgr_code LIKE 'General Manager%') > 0 THEN 'General Manager%'
                           WHEN
                              (SELECT COUNT(1)
                               FROM   #T2
                               WHERE  project_mgr_code LIKE 'General Manager%') = 0
                              AND
                                 (SELECT COUNT(1)
                                  FROM   #T2
                                  WHERE  project_mgr_code LIKE 'CE%') > 0 THEN 'CE%'
                           WHEN
                              (SELECT COUNT(1)
                               FROM   #T2
                               WHERE  project_mgr_code LIKE 'General Manager%') = 0
                              AND
                                 (SELECT COUNT(1)
                                  FROM   #T2
                                  WHERE  project_mgr_code LIKE 'CE%') = 0
                              AND
                                 (SELECT COUNT(1)
                                  FROM   #T2
                                  WHERE  project_mgr_code LIKE 'Director%') > 0 THEN 'Director%'
                        END
      OR (project_mgr_code NOT LIKE('General Manager%')
         AND project_mgr_code NOT LIKE('CE%')
         AND project_mgr_code NOT LIKE('Director%')
        );

结果:

enter image description here