求救-这个SQL怎么写,24小时内结帖
现在有两个表
表Sales
品名(PK)
年月(PK)
数量
表Cost
品名(PK)
年月(PK)
价格
表Sales 中保存2002年到2009中的贩卖计划
表Cost中保存2004年到2006中的`商品价格
在进行金额计算时,要求对于表Cost中不存在的数据,使用最老的数据进行计算
即使用同一品名中年月值最小的价格进行计算(并不一定是2004年1月的价格)
------------
select 品名,年月,数量,数量*价格
---------------------
这个SQL怎么写?
问题点数:100、回复次数:5Top
1 楼ZFL_progress(QQ:514788921)回复于 2005-03-01 00:17:16 得分 20
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)
如果错的话请再表达清楚点!
Top
2 楼bzszp(SongZip)回复于 2005-03-01 08:43:11 得分 20
try:
select 品名,年月,数量*nvl(价格,select min(价格) from cost c where c.品名=t.品名)
from
(select 品名,年月,数量,价格
from sales a,cost b
where a.品名=b.品名(+) and a.年月=b.年月(+)
) tTop
3 楼lynx(lynx)回复于 2005-03-01 10:17:01 得分 60
要求对于表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.年月(+)) tTop
4 楼sdlining(火蝙蝠)回复于 2005-03-01 10:18:27 得分 0
可能我讲的不太明白,比如说产品A 在2003年3月卖出1000个,但是由于在Cost表中没有产品A在2003年3月的价格数据,所以采用价格表中最早的产品A的价格进行计算(年月最小),比如价格表中有2005年1月到2006年12月产品A的价格数据,则采用2005年1月的数据来计算金额.Top
5 楼sdlining(火蝙蝠)回复于 2005-03-01 11:10:28 得分 0
谢谢lynx(lynx),你的方法可行,不过由于数据量太大,(sales > 10000000,cost > 1000000),服务器几乎死机,我再想想其他办法,谢谢各位帮忙.Top





