学习分析函数LAG以后,马上学以致用 帮一网友解决问题.

QUOTE:
原帖由 lypch 于 2008-7-12 11:28 发表 screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new windownCTRL+Mouse wheel to zoom in/out';}" οnclick="if(!this.resized) {return true;} else {window.open(this.src);}" src="http://www.itpub.net/images/common/back.gif" οnlοad="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new windownCTRL+Mouse wheel to zoom in/out';}" border="0" />
LEVEL Parent Child Parent Qty Child Qty
1 A B 1 3
2 B C 2 3
3 C D 5 6
4 D E 1 2
1 A Z 1 3

A是成品
B,C,D是半成品
E,Z是原材料

从上面一个比例关系可以计算出,做一个A最终需要10.8个E和3个Z,
也就是能看到下面的结果
Parent Child QTY
A E 10.8
A Z 3

我想知道有没有什么办法通过一个SQL语句来实现这个功能。

请大家帮忙想象办法。

搭测试环境:
create table T_T
(
T0 int
,parent varchar2(2)
,chile varchar2(2)
,parentQty int
,childQty int
)
insert into T_T values(1,'A','B',1,3);
insert into T_T values(2,'B','C',2,3);
insert into T_T values(3,'C','D',5,6);
insert into T_T values(4,'D','E',1,2);
insert into T_T values(1,'A','Z',1,3);
insert into T_T values(1,'H','I',1,3);
insert into T_T values(2,'I','J',1,3);
insert into T_T values(3,'G','H',1,3);
insert into T_T values(1,'L','M',1,2);
insert into T_T values(2,'M','N',1,9);
commit;


SQL:应用lag分析函数:
select aa.a,aa.c,aa.d,bb.h*decode(bb.i,0,1,bb.i)*decode(bb.j,0,1,bb.j)*decode(bb.k,0,1,bb.k) FROM
(
select B,min(parent) c,max(chile) d,A from(
select T0,parent,chile,
SUM(p) over(PARTITION BY A ORDER BY parent) B,A
FROM (
select T0,parent,chile,lag(chile) over(partition by a order by parent) C, (case when lag(chile) over(partition by a order by parent)=parent then 0 else 1 end ) P,B,A
from
(select T0,parent,chile,(case ASCII (chile)-ASCII (parent) when 1 then 1 else 2 end) A,childqty/parentqty B from t_t
order by parent)))
group by B,A)
AA
LEFT OUTER JOIN
(select A,C,sum(case T0 when 1 then B else 0 end) h,
sum(case T0 when 2 then B else 0 end) i,
sum(case T0 when 3 then B else 0 end) j,
sum(case T0 when 4 then B else 0 end) k
from(
select T0,parent,chile,B,
SUM(p) over(PARTITION BY A ORDER BY parent) C,A
FROM (
select T0,parent,chile,lag(chile) over(partition by a order by parent) C, (case when lag(chile) over(partition by a order by parent)=parent then 0 else 1 end ) P,B,A
from
(select T0,parent,chile,(case ASCII (chile)-ASCII (parent) when 1 then 1 else 2 end) A,childqty/parentqty B from t_t
order by parent)))
group by a,C) BB On aa.a||aa.b=bb.a||bb.c

BEFORE:
1 1 A B 1 3
2 1 A Z 1 3
3 2 B C 2 3
4 3 C D 5 6
5 4 D E 1 2
6 3 G H 1 3
7 1 H I 1 3
8 2 I J 1 3
9 1 L M 1 2
10 2 M N 1 9


RESULT:
1 1 A E 10.8
2 1 G J 27
3 1 L N 18
4 2 A Z 3[@more@]

但是还有一种情况,以上没有办法解决

如果多条数据为:

A F 1 3

抓出来的数据就会问题,这个BUG日后在想想看看有更好的解决办法没!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/101162/viewspace-1007635/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/101162/viewspace-1007635/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值