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 | 66142 9 | 65126 11 | 62033 8 | 58355 12 | 56115 13 | 49680 19 | 46793 18 | 46364 7 | 46351 14 | 45361 20 | 45171 17 | 44906 15 | 43814 16 | 43568 21 | 42552 22 | 37962 6 | 32810 23 | 32586 24 | 26960 25 | 21090 5 | 19658 26 | 16043 27 | 11713 4 | 9809 28 | 8382 29 | 5634 3 | 4038 30 | 3772 31 | 2335 32 | 1519 2 | 1172 33 | 857 34 | 502 35 | 294 1 | 216 36 | 132 37 | 70 38 | 43 39 | 25 40 | 12 41 | 6 44 | 3 42 | 3 43 | 2 (45 rows) COMMIT; COMMIT