表1物品名 数量毛巾 10床单 21枕巾 2毛巾 13毛巾 5枕巾 3表2物品名 数量床单 2枕巾 1枕巾 1
我想用表1 物品名 的 数量的总和 减去表2中相同 物品名 数量的总和 , 在查询中显示
查询
物品名 数量毛巾 28床单 19枕巾 3
另外:如果是语句且附后access的是怎么写。如果是用查询生成器如何操作。

解决方案 »

  1.   

    select 物品名,sum(数量) from(
    select 物品名,数量 from 表1 union all select 物品名,数量 * -1 from 表2) group by 物品名试试看,可以不。。
      

  2.   

    SELECT a.物品名, a.数量 - nvl(b.数量, 0) 数量
      FROM (SELECT 物品名, SUM(数量) 数量 FROM 表1 GROUP BY 物品名) a,
           (SELECT 物品名, SUM(数量) 数量 FROM 表2 GROUP BY 物品名) b
     WHERE a.物品名 = b.物品名(+)
      

  3.   

    with a as(
    select '毛巾' name,10 qty from dual
    union all
    select '床单' name,21 qty from dual
    union all
    select '枕巾' name,2 qty from dual
    union all
    select '毛巾' name,13 qty from dual
    union all
    select '毛巾' name,5 qty from dual
    union all
    select '枕巾' name,3 qty from dual
    ),b as(
    select '床单' name,2 qty from dual
    union all
    select '枕巾' name,1 qty from dual
    union all
    select '枕巾' name,1 qty from dual
    )
    select aname,decode(aqty-bqty,null,aqty,aqty-bqty) from
    (select name aname,sum(qty) aqty from a group by name) a
    ,
    (select name bname,sum(qty) bqty from b group by name) b
    where aname = bname(+)