1

我有两张桌子,上面有售出物品的数量。我需要比较这两个表并获取不匹配的记录,一个表中售出的商品总和不等于另一表中售出的单位数量

select * from 
(select sum(units) AS UNITS, item, location, tran_date from tran_data_history where tran_date  = '' and tran_code = 1 group by item, location,tran_date)A,
(select sum(qty) AS QTY, item, store from sa_tran_item where  tran_Seq_no =''
)B
where A.item = B.item and A.location = B.store and A.UNITS <> B.QTY;

它给了我两个表中项目数不匹配的行。但我想要那些出现在一张桌子上而不出现在另一张桌子上的商品、商店组合。

例如 tran_data_history

item location units
11     a        5
22      b        1
33      c        4

sa_tran_item

item  store  qty
11      a     4
33      c      4

在 sa_tran_item 中,第 33 项未发布,我想显示行

item  store  qty  units
11      a     4    5
22      b      0    1

请帮忙

4

2 回答 2

0
select * from 
(select sum(units) AS UNITS, item, location, tran_date from tran_data_history where tran_date  = '' and tran_code = 1 group by item, location,tran_date) A
    full outer join 
(select sum(qty) AS QTY, item, store from sa_tran_item where  tran_Seq_no ='')B
on A.item = B.item and A.location = B.store 
where A.UNITS <> B.QTY;
于 2012-11-14T14:30:05.947 回答
0

我现在没有安装 DB 的计算机,但您似乎想要进行 FULL OUTER JOIN。请参阅SQL JOIN Wikipedia 条目的 FULL OUTER JOIN 部分。此代码应允许您满足您的要求:

select 
    * 
from 
    (
        select 
            sum(units) AS UNITS
            , item
            , location
            , tran_date 
        from 
            tran_data_history 
        where 
            tran_date  = '' 
            and tran_code = 1 
        group by 
            item
            , location
            ,tran_date
    )A
FULL OUTER JOIN
    (
        select 
            sum(qty) AS QTY
            , item
            , store 
        from 
            sa_tran_item 
        where  
            tran_Seq_no =''
    )B 
 ON
 A.item = B.item 
 and A.location = B.store
 and and A.UNITS <> B.QTY
/
于 2012-11-14T14:30:25.127 回答