select 品名,年月,数量,数量*价格 from sales a,cost b where a.品名=b.品名
union
select 品名,年月,数量,数量*价格 from sales a,(select * from cost where 年月 in 
(select min(年月) from cost group by 品名)) where a.品名 not in 
(select 品名 from cost)如果错的话请再表达清楚点!

解决方案 »

  1.   

    try:
    select 品名,年月,数量*nvl(价格,select min(价格) from cost c where c.品名=t.品名) 
    from
    (select 品名,年月,数量,价格
    from sales a,cost b
    where a.品名=b.品名(+) and a.年月=b.年月(+)
    ) t
      

  2.   

    要求对于表Cost中不存在的数据,使用最老的数据进行计算?
    使用同一品名中年月值最小的价格进行计算???是在Cost表中吗?
    select 品名,
           年月,
           数量 * nvl(价格,
               (select 价格
                  from cost c1
                 where c1.年月 =
                       (select min(年月) from cost c where c.品名 = t.品名)))
      from (select 品名, 年月, 数量, 价格
              from sales a, cost b
             where a.品名 = b.品名(+)
               and a.年月 = b.年月(+)) t
      

  3.   

    可能我讲的不太明白,比如说产品A 在2003年3月卖出1000个,但是由于在Cost表中没有产品A在2003年3月的价格数据,所以采用价格表中最早的产品A的价格进行计算(年月最小),比如价格表中有2005年1月到2006年12月产品A的价格数据,则采用2005年1月的数据来计算金额.
      

  4.   

    谢谢lynx(lynx),你的方法可行,不过由于数据量太大,(sales > 10000000,cost > 1000000),服务器几乎死机,我再想想其他办法,谢谢各位帮忙.