BEGIN; BEGIN select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('24', '27', '29', '23', '17', '30', '32') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '27', '29', '23', '17', '30', '32') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 17 | 9075 | 6.81245e+07 23 | 8989 | 6.76329e+07 24 | 8945 | 6.70796e+07 27 | 9019 | 6.7666e+07 29 | 9208 | 6.90603e+07 30 | 9350 | 7.01539e+07 32 | 9110 | 6.84601e+07 (7 rows) COMMIT; COMMIT