需求:将表serial_bal中多行item_code相同的online_qty列求和再与表wh_bal中相同item_code的online_qty对比,查询不相等的数据
1.对表serial_bal使用sum()、group by求和
2.使用左连接,连接表wh_bal
3.筛选不相等数据
select a.item_code, totalAmount , b.online_qty
from (select item_code, sum(a.online_qty) as TotalAmount
from serial_bal a
group by item_code
) a left join
(select item_code,online_qty
from wh_bal b
) b
on a.item_code = b.item_code
where TotalAmount != b.online_qty