简体   繁体   English

检查组中的不一致值

[英]Checking for inconsistent values within groups

Consider the following table 考虑下表

 document_id    group_id    level
 --------------------------------
 1              1           1
 2              1           1
 3              2           3
 4              2           3
 5              2           2
 6              3           1
 7              4           6
 8              4           6
 9              4           6
10              5           2
11              5           1

I would like to check if the level of documents within one group_id is consistent, and if not, display those groups. 我想检查level的文件之一内group_id是一致的,如果没有,显示这些群体。 So for the above example the output would have to be: 因此,对于上面的示例,输出必须为:

 document_id    group_id    level
 --------------------------------
 3              2           3
 4              2           3
 5              2           2
10              5           2
11              5           1

How can I achieve this? 我该如何实现? Any help would be appreciated. 任何帮助,将不胜感激。

Use EXISTS to find these rows where same group_id has inconsistent level values: 使用EXISTS查找相同group_id的级别值不一致的行:

select document_id, group_id, level
from tablename t1
where exists (select 1 from tablename t2
              where t2.group_id = t1.group_id
                and t2.level <> t1.level)

And this is how it executes: 这是它的执行方式:

SQL>create table t (document_id int, group_id int, level int);
SQL>insert into t values( 1, 1, 1);
SQL>insert into t values( 2, 1, 1);
SQL>insert into t values( 3, 2, 3);
SQL>insert into t values( 4, 2, 3);
SQL>insert into t values( 5, 2, 2);
SQL>insert into t values( 6, 3, 1);
SQL>insert into t values( 7, 4, 6);
SQL>insert into t values( 8, 4, 6);
SQL>insert into t values( 9, 4, 6);
SQL>insert into t values(10, 5, 2);
SQL>insert into t values(11, 5, 1);
SQL>select document_id, group_id, level
SQL&from t t1
SQL&where exists (select 1 from t t2
SQL&              where t2.group_id = t1.group_id
SQL&                and t2.level <> t1.level);
document_id    group_id       level
=========== =========== ===========
          3           2           3
          4           2           3
          5           2           2
         10           5           2
         11           5           1

                  5 rows found

Same result as specified in question. 结果与所指定的相同。

Modified jarlh's answer to make query result match OP's desired output: 修改了jarlh的答案,以使查询结果与OP的期望输出匹配:

Use EXISTS to find these rows where same group also has another levels: 使用EXISTS查找这些行,其中同一组也具有另一个级别:

select document_id, group_id, level
from tablename t1
where exists (select 1 from tablename t2
              where t2.level <> t1.level
                and t2.group_id = t1.group_id)

As an alternative to exists , you can use window funtions: 作为一种替代exists ,你可以使用窗口功能:甲

select document_id, group_id, level
from (select t.*, min(level) over (partition by group_id) as minlevel,
             max(level) over (partition by group_id) as maxlevel
      from t
     ) t
where minlevel <> maxlevel;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM