嗨!你看看我下面的SQL,是不是你想要的
差额
select 'asa', '111', '差额', a.comcode, a.comname, a.itemdcode, b.itemname, b.speci, a.TQTY, b.qtyunit, '0', '2000-01-01', a.shelfno, 'HERE', '2000-01-01', serialno, lotno
from (
Select itemdcode, comcode, comname, shelfno, serialno, lotno, sum(aa)-SUM(bb) AS TQTY
from (
Select itemdcode, comcode, comname, shelfno, serialno, lotno, sum(aa)-SUM(bb) AS TQTY
from (
select itemdcode, comcode, comname, shelfno, serialno, lotno, sum(qty) AS aa, 0 AS bb
from t_count
where optype in ('实盘')
group by itemdcode, comcode, comname, shelfno, serialno, lotno
union
select itemdcode, comcode, comname, shelfno, serialno, lotno, 0 AS aa, sum(qty) AS bb
from t_count
where optype in ('机盘')
group by itemdcode, comcode, comname, shelfno, serialno, lotno )
group by itemdcode, comcode, comname, shelfno, serialno, lotno )
where not TQTY<>0
) AS a INNER JOIN t_itembase AS b ON a.itemdcode=b.itemdcode