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%requests%' 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 | 66192 9 | 65089 11 | 62055 8 | 58488 12 | 56010 13 | 49694 19 | 46647 18 | 46248 7 | 46237 20 | 45254 14 | 45204 17 | 44975 16 | 43916 15 | 43854 21 | 42224 22 | 38218 6 | 32844 23 | 32705 24 | 26784 25 | 21105 5 | 19657 26 | 16113 27 | 11698 4 | 9801 28 | 8380 29 | 5623 3 | 4017 30 | 3752 31 | 2318 32 | 1519 2 | 1175 33 | 875 34 | 492 35 | 307 1 | 227 36 | 127 37 | 66 38 | 37 39 | 24 40 | 16 41 | 5 42 | 4 45 | 1 44 | 1 43 | 1 (46 rows) COMMIT; COMMIT