with tmp as( select c.item_id from (select b.item_id, b.sum_amt, rank()over(order by b.sum_amt desc) rnk from (select item_id, sum(amt) sum_amt from (select buyer_id, item_id, amt from A where month(dt) in (7,8,9) and seller_id = '123' )a group by item_id )b )c where c.rnk = 1) select buyer_id, item_id from A where item id = (select item_id from tmp) group by buyer_id, item_id order by SUM(amt) desc limit 1