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 | 66215 9 | 65063 11 | 62043 8 | 58449 12 | 55915 13 | 49772 19 | 46634 18 | 46330 7 | 46286 14 | 45215 20 | 45202 17 | 44925 15 | 43910 16 | 43870 21 | 42266 22 | 38208 6 | 32855 23 | 32713 24 | 26793 25 | 21048 5 | 19664 26 | 16169 27 | 11679 4 | 9791 28 | 8385 29 | 5585 3 | 4021 30 | 3771 31 | 2338 32 | 1520 2 | 1170 33 | 867 34 | 488 35 | 311 1 | 226 36 | 125 37 | 70 38 | 34 39 | 25 40 | 16 41 | 5 42 | 4 45 | 1 44 | 1 43 | 1 (46 rows) COMMIT; COMMIT