结合嵌套选择查询和另一查询的SQL

结合嵌套选择查询和另一查询的SQL,sql,oracle11g,Sql,Oracle11g,我在组合与同一项相关的以下两个查询时遇到一些问题: 一, 此查询获取有关项目的常规信息 二, 第二个计算某个日期该项目的余额 我希望能够将第二次查询的结果显示为第一次查询中的一列 select * from (select inventory.itemnum as itemnum, item.description as itemdesc, inventory.minlevel as olevel , invcost.avgcost from inventory join invcost on

我在组合与同一项相关的以下两个查询时遇到一些问题:

一,

此查询获取有关项目的常规信息

二,

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

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

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
像这样试试

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

我尝试了上面的查询,但它告诉我:ORA-01789:查询块的结果列数不正确。尽管我检查了它们各自给出的数字是否相同,但我相信它来自这一行:select SUMbalvalues,itemnum from,因为itemnum不可访问。我该如何解决这个问题?@AuthenticReplica你可以创建sql Fiddle我尝试了上面的查询,但它告诉我:ORA-01789:查询块的结果列数不正确。尽管我检查了它们各自给出的数字是否相同,但我相信它来自这一行:select SUMbalvalues,itemnum from,因为itemnum不可访问。我该如何解决这个问题?@AuthenticReplica你能创建sql fiddle吗
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