SELECT cntrycode, Count(1) AS numcust, Sum(c_acctbal) AS totacctbal FROM   (SELECT Substring(c_phone FROM 1 FOR 2) AS cntrycode, c_acctbal FROM   demo_local.customer_base LEFT JOIN demo_local.orders_mergekey ON o_custkey = c_custkey WHERE  Substring(c_phone FROM 1 FOR 2) IN ( '13', '31', '23', '29', '30', '18', '17' ) AND c_acctbal > (SELECT Avg(c_acctbal) FROM demo_local.customer_base WHERE c_acctbal > 0.00 AND Substring (c_phone FROM 1 FOR 2) IN ( '13', '31', '23', '29','30', '18', '17' )) AND o_custkey IS NULL ) GROUP  BY cntrycode ORDER  BY cntrycode;
