SQL将嵌套选择查询与另一个查询相结合

时间:2015-08-27 05:04:38

标签: sql oracle11g

我在处理与同一项目相关的以下两个查询时遇到了一些问题:

1)

select inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost
from inventory join invcost on inventory.itemnum = invcost.itemnum
join item on inventory.itemnum = item.itemnum
where inventory.location = 'CENTRAL' AND inventory.itemnum = 'XMP-3500';

此查询提取有关项目的一般信息

2)

select SUM(balvalues) from
(
    select INVBALANCES.CURBAL as balvalues from invbalances join inventory
    on invbalances.itemnum = inventory.itemnum and invbalances.location =
    inventory.location where inventory.itemnum = 'XMP-3500'
    UNION ALL
    SELECT -1 * SUM(QUANTITY) from matrectrans where itemnum = 'XMP-3500'
    and TRANSDATE >= '16-SEP-99' and tostoreloc = 'CENTRAL'
    UNION ALL
    SELECT -1 * SUM(QUANTITY) from matusetrans where itemnum = 'XMP-3500'
    and TRANSDATE >= '16-SEP-99' and storeloc = 'CENTRAL'
);

第二个计算某个日期该项目的余额。

我希望能够将第二个查询的结果显示为第一个

中的列

1 个答案:

答案 0 :(得分:0)

试试这样..

select * from (select inventory.itemnum as itemnum, item.description as itemdesc,
inventory.minlevel as olevel , invcost.avgcost
from inventory join invcost on inventory.itemnum = invcost.itemnum
join item on inventory.itemnum = item.itemnum
where inventory.location = 'CENTRAL' AND inventory.itemnum = 'XMP-3500'; ) 
table1 join (select SUM(balvalues),itemnum from
(
    select INVBALANCES.CURBAL as balvalues,inventory.itemnum as itemnum from
    invbalances join inventory
    on invbalances.itemnum = inventory.itemnum and invbalances.location =
    inventory.location where inventory.itemnum = 'XMP-3500'
    UNION ALL
    SELECT -1 * SUM(QUANTITY) from matrectrans where itemnum = 'XMP-3500'
    and TRANSDATE >= '16-SEP-99' and tostoreloc = 'CENTRAL'
    UNION ALL
    SELECT -1 * SUM(QUANTITY) from matusetrans where itemnum = 'XMP-3500'
    and TRANSDATE >= '16-SEP-99' and storeloc = 'CENTRAL'
) table2 on table1.itemnum= table2.itemnum