,
在第二个FROM
删除它之前有一个额外的:
Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
Sum(tbltrans2_temp.qty) qty
From
tbltrans_temp Inner Join
tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
where
tbltrans_temp.saleDate='11/07/2013'
Group By
tbltrans2_temp.itemcode
UNION
Select
tbltrans2.itemcode,
tbltrans2.itemname,
Sum(tbltrans2.qty) qty, <<<<------------------- This
From
tbltrans Inner Join
tbltrans2 On tbltrans2.transid = tbltrans.transid
where
tbltrans.saleDate='11/07/2013'
Group By
tbltrans2.itemcode
更新
要从两个表中获取总和qty
,将该查询放在子查询中并将其求和到外部查询中,您也可以sum
在内部查询中省略 并且只在外部查询中执行:
SELECT
itemcode,
itemname,
SUM(qty) TotalQty
FROM
(
Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
Sum(tbltrans2_temp.qty) qty
From
tbltrans_temp Inner Join
tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
where
tbltrans_temp.saleDate='11/07/2013'
Group By
tbltrans2_temp.itemcode
UNION
Select
tbltrans2.itemcode,
tbltrans2.itemname,
Sum(tbltrans2.qty) qty
From
tbltrans Inner Join
tbltrans2 On tbltrans2.transid = tbltrans.transid
where
tbltrans.saleDate='11/07/2013'
Group By
tbltrans2.itemcode
) t
GROUP BY itemcode, itemname;