日期:2014-05-17 浏览次数:21045 次
create table t(
bom_code varchar2(20),
lrsl number(19,2),
price number(19,4),
je number(19,4),
fsrq date,
gys_code varchar2(20)
);
insert into t values('物料1',200,5,1000,to_date('2012.01.01','yyyy.mm.dd'),'供应商1');
insert into t values('物料1',100,10,1000,to_date('2012.01.02','yyyy.mm.dd'),'供应商2');
insert into t values('物料1',2000,4,8000,to_date('2012.01.03','yyyy.mm.dd'),'供应商3');
insert into t values('物料1',100,3,300,to_date('2012.02.01','yyyy.mm.dd'),'供应商1');
insert into t values('物料1',300,7,2100,to_date('2012.02.02','yyyy.mm.dd'),'供应商2');
insert into t values('物料1',200,5,1000,to_date('2012.02.03','yyyy.mm.dd'),'供应商3');
insert into t values('物料2',200,5,1000,to_date('2012.04.01','yyyy.mm.dd'),'供应商1');
insert into t values('物料2',100,10,1000,to_date('2012.04.02','yyyy.mm.dd'),'供应商2');
insert into t values('物料2',2000,4,8000,to_date('2012.04.03','yyyy.mm.dd'),'供应商3');
insert into t values('物料2',100,3,300,to_date('2012.02.01','yyyy.mm.dd'),'供应商1');
insert into t values('物料2',300,7,2100,to_date('2012.02.02','yyyy.mm.dd'),'供应商2');
insert into t values('物料2',200,5,1000,to_date('2012.02.03','yyyy.mm.dd'),'供应商3');
select t.bom_code 物料代码,
t.gys_code 供应商名称,
min(t.fsrq) 起始日期,
sum(t.lrsl) 订货数量,
sum(t.lrsl*t.price) 订货金额,--如果你的je正确,也可以sum(t.je)
max(t.price) 最高价格,
avg(t.price) 平均价格,
min(t.price) 最低价格,
max(t2.price) 最新价格
from t,
(select t1.bom_code,t1.gys_code,price from t,
(select bom_code,gys_code,max(fsrq) fsrq
from t group by bom_code,gys_code) t1
where t.bom_code=t1.bom_code
and t.gys_code=t1.gys_code and t.fsrq=t1.fsrq) t2
where t.bom_code=t2.bom_code and t.gys_code=t2.gys_code
group by t.bom_code,t.gys_code
order by t.bom_code,t.gys_code;
/
------解决方案--------------------
--最新价格 可以采用分析函数来算,效率要高一点:(示例数据如#2)
select bom_code 物料代码
,gys_code 供应商名称
,min(fsrq) 起始日期
,sum(lrsl) 订货数量
,sum(je) 订货金额
,max(price) 最高价格
,avg(price) 平均价格
,min(price) 最低价格
,min(price) keep (dense_rank first order by fsrq desc) 最新价格
from t
group by bom_code,gys_code;
------解决方案--------------------
亲自动手尝试了,才知道那个好