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 | 66170 9 | 65179 11 | 62060 8 | 58528 12 | 55898 13 | 49823 19 | 46650 7 | 46437 18 | 46336 14 | 45198 20 | 45193 17 | 45010 16 | 43987 15 | 43930 21 | 42200 22 | 38120 6 | 32971 23 | 32555 24 | 26705 25 | 20985 5 | 19779 26 | 16039 27 | 11601 4 | 9855 28 | 8309 29 | 5542 3 | 4047 30 | 3725 31 | 2317 32 | 1513 2 | 1177 33 | 849 34 | 483 35 | 304 1 | 228 36 | 120 37 | 69 38 | 35 39 | 24 40 | 16 42 | 5 41 | 5 45 | 1 44 | 1 (45 rows) COMMIT; COMMIT