select x.cid, x.pid, x.aid, x.ord_qty, y.avg_qty
  from (select cid, pid, aid, sum(qty) ord_qty
          from orders
         group by cid, pid, aid) x,
       (select aid, pid, avg(qty) avg_qty
          from orders
         group by aid,pid) y
 where x.aid=y.aid
   and x.pid=y.pid
   and ord_qty > avg_qty;is it ok?