with t1 as ( select goods_id ,sum(count) total from trans group by 1 having sum(count)>20 ) select id ,name ,weight ,total from goods t2 join t1 on t2.id = t1.goods_id where weight<50 order by 1