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 | 65965 9 | 65344 11 | 62358 8 | 58404 12 | 55928 13 | 49729 19 | 46624 7 | 46403 18 | 46196 20 | 45468 14 | 45332 17 | 45126 16 | 43830 15 | 43746 21 | 42415 22 | 37832 6 | 32967 23 | 32502 24 | 26744 25 | 21016 5 | 19774 26 | 15984 27 | 11677 4 | 9875 28 | 8135 29 | 5681 3 | 4050 30 | 3762 31 | 2300 32 | 1495 2 | 1179 33 | 851 34 | 492 35 | 269 1 | 227 36 | 143 37 | 71 38 | 34 39 | 24 40 | 16 41 | 7 45 | 2 43 | 2 42 | 2 44 | 1 (46 rows) COMMIT; COMMIT