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%accounts%' 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 | 500018 10 | 65913 9 | 65378 11 | 62470 8 | 58331 12 | 55786 13 | 49787 19 | 46631 7 | 46473 18 | 46186 20 | 45451 14 | 45401 17 | 45065 16 | 43883 15 | 43700 21 | 42424 22 | 37917 6 | 32925 23 | 32381 24 | 26794 25 | 20989 5 | 19795 26 | 16028 27 | 11664 4 | 9885 28 | 8137 29 | 5663 3 | 4034 30 | 3758 31 | 2314 32 | 1479 2 | 1177 33 | 872 34 | 487 35 | 266 1 | 232 36 | 144 37 | 75 38 | 30 39 | 29 40 | 15 41 | 6 45 | 2 43 | 2 42 | 2 44 | 1 (46 rows) COMMIT; COMMIT