BEGIN; BEGIN select c_count, count(*) as custdist from ( select c_custkey, count(o_orderkey) from customer left outer join orders on c_custkey = o_custkey and o_comment not like '%express%packages%' group by c_custkey ) as c_orders (c_custkey, c_count) group by c_count order by custdist desc, c_count desc; c_count | custdist ---------+---------- 0 | 500021 10 | 66077 9 | 65186 11 | 62133 8 | 58314 12 | 55921 13 | 49886 19 | 46903 7 | 46391 18 | 46300 14 | 45427 20 | 45084 17 | 44775 16 | 43684 15 | 43665 21 | 42454 22 | 38146 6 | 32796 23 | 32543 24 | 26885 25 | 21125 5 | 19597 26 | 16126 27 | 11702 4 | 9888 28 | 8274 29 | 5704 3 | 4015 30 | 3766 31 | 2343 32 | 1522 2 | 1174 33 | 859 34 | 506 35 | 288 1 | 218 36 | 137 37 | 69 38 | 43 39 | 28 40 | 11 42 | 5 41 | 5 44 | 2 45 | 1 43 | 1 (46 rows) COMMIT; COMMIT