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 ('13', '31', '27', '24', '10', '23', '28') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('13', '31', '27', '24', '10', '23', '28') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 10 | 9072 | 6.78473e+07 13 | 9029 | 6.76125e+07 23 | 8990 | 6.76379e+07 24 | 8951 | 6.71096e+07 27 | 9024 | 6.76909e+07 28 | 9037 | 6.771e+07 31 | 9099 | 6.82095e+07 (7 rows) COMMIT; COMMIT