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 ('14', '24', '26', '19', '27', '28', '33') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('14', '24', '26', '19', '27', '28', '33') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; cntrycode | numcust | totacctbal -----------+---------+------------- 14 | 9020 | 6.78364e+07 19 | 9100 | 6.82873e+07 24 | 8948 | 6.70946e+07 26 | 9026 | 6.77699e+07 27 | 9021 | 6.76759e+07 28 | 9036 | 6.7705e+07 33 | 9187 | 6.89201e+07 (7 rows) COMMIT; COMMIT