Pr0no
Pr0no

Reputation: 4109

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. 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.

Upvotes: 0

Views: 466

Answers (3)

jarlh
jarlh

Reputation: 44796

Use EXISTS to find these rows where same group_id has inconsistent level values:

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.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1270603

As an alternative to exists, you can use window funtions:

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;

Upvotes: 1

user5226582
user5226582

Reputation: 1986

Modified jarlh's answer to make query result match OP's desired output:

Use EXISTS to find these rows where same group also has another levels:

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)

Upvotes: 1

Related Questions