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 '%unusual%deposits%' 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 | 500019 10 | 66135 9 | 65135 11 | 62271 8 | 58245 12 | 55607 13 | 50202 19 | 46582 7 | 46473 18 | 46290 20 | 45277 14 | 45147 17 | 44839 15 | 43979 16 | 43584 21 | 42515 22 | 38090 6 | 32721 23 | 32616 24 | 26835 25 | 21308 5 | 19605 26 | 15916 27 | 11768 4 | 9972 28 | 8199 29 | 5657 3 | 3979 30 | 3739 31 | 2385 32 | 1547 2 | 1173 33 | 873 34 | 493 35 | 282 1 | 238 36 | 134 37 | 82 38 | 41 39 | 21 40 | 11 41 | 7 42 | 4 45 | 2 43 | 2 (45 rows) COMMIT; COMMIT