--
-- ### advanced group check  
-- 

drop if exists table t1;
create table t1 (
    a int,
    b int,
    c int);

insert into t1 values ( 1, 1, 10 );
insert into t1 values ( 1, 1, 20 );
insert into t1 values ( 2, 1, 5 );
insert into t1 values ( 2, 1, 42 );
insert into t1 values ( 2, 1, 42 );
insert into t1 values ( 3, 1, 1 );
insert into t1 values ( 3, 1, 65 );
insert into t1 values ( 3, 1, 12 );

drop if exists table t2;
create table t2 (
    a int,
    b int,
    c int);

insert into t2 values ( 1, 1, 10 );
insert into t2 values ( 1, 1, 20 );
insert into t2 values ( 2, 2, 5 );
insert into t2 values ( 2, 2, 42 );
insert into t2 values ( 2, 2, 42 );

select
    a as a,
    b as b,
    sum(c)
from t1 group by a, b
having sum(a) = 2;

select t2.a, t2.b, t2.c from t2 where exists (
select
    t1.a as a,
    sum(t1.c)
from t1 where b = t2.b
group by a having sum(c) = 30 );
