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 | 66182 9 | 65225 11 | 61962 8 | 58473 12 | 56065 13 | 49770 19 | 46865 7 | 46457 18 | 46449 14 | 45310 20 | 45148 17 | 44888 15 | 43888 16 | 43670 21 | 42511 22 | 37816 6 | 32968 23 | 32501 24 | 26822 25 | 20980 5 | 19733 26 | 15959 27 | 11625 4 | 9881 28 | 8304 29 | 5582 3 | 4069 30 | 3739 31 | 2316 32 | 1508 2 | 1184 33 | 840 34 | 493 35 | 291 1 | 216 36 | 129 37 | 67 38 | 42 39 | 25 40 | 12 41 | 6 44 | 3 42 | 3 43 | 2 (45 rows) COMMIT; COMMIT