ITPUB??ì3
新一届的微软MVP评选已经开始,欢迎各位推荐!
ITPUB论坛 » Oracle数据库管理 » 能解这个的,一定是SQL高手。

标题: 能解这个的,一定是SQL高手。
离线 lypch
初级会员



精华贴数 0
个人空间 0
技术积分 174 (10622)
社区积分 0 (123093)
注册日期 2004-7-8
论坛徽章:0
      
      

发表于 2008-7-12 11:28 
能解这个的,一定是SQL高手。

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语句来实现这个功能。

请大家帮忙想象办法。


只看该作者    顶部
离线 kso
Just do it!


精华贴数 0
个人空间 0
技术积分 2428 (646)
社区积分 114 (3194)
注册日期 2005-5-6
论坛徽章:17
ITPUB元老红孩儿    
      

发表于 2008-7-12 12:26 
BOM结构啊。。。


__________________
执子之手,与子偕老。
只看该作者    顶部
离线 mihawk
卡卡西


来自 杭州
精华贴数 1
个人空间 0
技术积分 1229 (1440)
社区积分 47 (5033)
注册日期 2004-12-27
论坛徽章:4
2008北京奥运纪念徽章:皮划艇激流回旋2008北京奥运纪念徽章:柔道2008北京奥运纪念徽章:羽毛球生肖徽章2007版:鸡  
      

发表于 2008-7-12 13:53 
只能得到算式, 等待高手出来继续.


select parent, child, '1'||x as x
from
(
select a.*, sys_connect_by_path(cqty/pqty, '*') as x
from
(
select 1 as lv, 'A' as parent, 'B' as child, 1 as pqty, 3 as cqty from dual union all
select 2 as lv, 'B' as parent, 'C' as child, 2 as pqty, 3 as cqty from dual union all
select 3 as lv, 'C' as parent, 'D' as child, 5 as pqty, 6 as cqty from dual union all
select 4 as lv, 'D' as parent, 'E' as child, 1 as pqty, 2 as cqty from dual union all
select 1 as lv, 'A' as parent, 'Z' as child, 1 as pqty, 3 as cqty from dual
) a
start with lv=1
connect by parent=prior child
)
where child in ('Z','E')
/



P C X
- - --------------------
D E 1*3*1.5*1.2*2
A Z 1*3


__________________
◆◆决定一个SQL执行效率的是执行计划, 而不是SQL的写法.◆◆
1. 数据真实的"统计"的分布情况
2. 系统视图中记录的统计信息
3. 实际每个SQL执行时对应的数据情况
只看该作者    顶部
离线 qiuruiping
中级会员


精华贴数 0
个人空间 0
技术积分 493 (3935)
社区积分 33 (5937)
注册日期 2002-1-24
论坛徽章:0
      
      

发表于 2008-7-12 14:01 

只看该作者    顶部
离线 yuanfang830
初级会员



精华贴数 0
个人空间 0
技术积分 186 (10035)
社区积分 0 (638255)
注册日期 2005-10-1
论坛徽章:0
      
      

发表于 2008-7-12 14:23 
SQL>  select * from test_0712;

LE PARENT CHILD PA CH
-- ------ ----- -- --
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

SQL>
SQL> select t1.parent,t4.child,t1.bili*t2.bili*t3.bili*t4.bili qty  from
  2  (select t.parent,t.child_qty/t.parent_qty bili from  test_0712 t where levell=1 ) t1,
  3  (select t.child_qty/t.parent_qty bili from  test_0712 t where levell=2 ) t2,
  4  (select t.child_qty/t.parent_qty bili from  test_0712 t where levell=3 ) t3,
  5  (select t.child,t.child_qty/t.parent_qty bili from  test_0712 t where levell=4 ) t4
  6  union
  7  select t5.parent,t5.child,t5.child_qty/t5.parent_qty bili from  test_0712 t5 where levell=1 and Child='Z'
  8  /

PARENT CHILD        QTY
------ ----- ----------
A      E           10.8
A      Z              3


只看该作者    顶部
离线 zhangweicai74
阿财


来自 贵州
精华贴数 3
个人空间 240
技术积分 5712 (232)
社区积分 21774 (59)
注册日期 2007-12-13
论坛徽章:155
授权会员生肖徽章2007版:虎生肖徽章2007版:龙   
      

发表于 2008-7-12 14:50 
如果层次不确定的话可能不好写


__________________
NEVER TOO LATE
想吃贵州家乡菜不?进来嘛,老乡!!
http://space.itpub.net/12391917/viewspace-257185
玩玩双节棍(l_l)

美丽家乡黄果树瀑布
http://space.itpub.net/12391917/viewspace-244134
只看该作者    顶部
离线 lypch
初级会员



精华贴数 0
个人空间 0
技术积分 174 (10622)
社区积分 0 (123093)
注册日期 2004-7-8
论坛徽章:0
      
      

发表于 2008-7-12 17:00 
借用楼上的话,层次多,且不确定的情况下,不好写。

如果下面的例子中,没有A - Z 的话,我可以用一句话写出来,利用Oracel的分析函数就可以实现,但是如果加了A-Z的话,就不行了。

顺便说一下,Level是一个伪列,再表中没有这个字段的,是通过层次查询中弄出来的。这里为了说明简单而标注上了。但再正式的表中是没有这个字段的。

QUOTE:
原帖由 yuanfang830 于 2008-7-12 14:23 发表
SQL>  select * from test_0712;

LE PARENT CHILD PA CH
-- ------ ----- -- --
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

SQL>
SQL> select t1.parent,t4.child,t1.bili*t2.bili*t3.bili*t4.bili qty  from
  2  (select t.parent,t.child_qty/t.parent_qty bili from  test_0712 t where levell=1 ) t1,
  3  (select t.child_qty/t.parent_qty bili from  test_0712 t where levell=2 ) t2,
  4  (select t.child_qty/t.parent_qty bili from  test_0712 t where levell=3 ) t3,
  5  (select t.child,t.child_qty/t.parent_qty bili from  test_0712 t where levell=4 ) t4
  6  union
  7  select t5.parent,t5.child,t5.child_qty/t5.parent_qty bili from  test_0712 t5 where levell=1 and Child='Z'
  8  /

PARENT CHILD        QTY
------ ----- ----------
A      E           10.8
A      Z              3




只看该作者    顶部
离线 anoblue
中级会员



精华贴数 1
个人空间 0
技术积分 764 (2473)
社区积分 4 (17144)
注册日期 2003-12-4
论坛徽章:1
授权会员     
      

发表于 2008-7-12 17:35 
select parent,child,level1,sys_connect_by_path(childqty/parentqty,'*') rn,
  strtonumber(1,substr(trim(sys_connect_by_path(childqty/parentqty,'*')),2))
  from tt start with (level1=1) CONNECT BY PRIOR  child=parent

strtonumber写的比较差,懒得完善了

create or replace function strtonumber(preadd number,str in varchar2 ) return number
as
n1 number;
n2 number;
strleft varchar2(100);
midadd number;
begin
  if str is null or str='' then
   return preadd ;
   end if;
  
select decode(substr(str,1,instr(str,'*')-1),null,0,substr(str,1,instr(str,'*')-1)) into n1 from dual;
  if n1=0 then
   select preadd*str into n1 from dual;
   return n1;
  end if;
  
  select substr(str,instr(str,'*')+1,decode(sign(instr(str,'*',1,2)-1),1,instr(str,'*',1,2)-instr(str,'*')-1,length(str)-instr(str,'*'))) into n2 from dual;
  midadd:=preadd*n1*n2;

  if(instr(str,'*',1,2)>1) then
  strleft:=substr(str,instr(str,'*',1,2)+1,length(str)-instr(str,'*',1,2));
  
  return strtonumber(midadd,strleft);
  else
   return midadd;
   end if;
end;


__________________
msn:anobluee@hotmail.com找兼职 最好是oracle开发 有时间来做9i Performance tuning|database administration fundamentals
只看该作者    顶部
离线 anoblue
中级会员



精华贴数 1
个人空间 0
技术积分 764 (2473)
社区积分 4 (17144)
注册日期 2003-12-4
论坛徽章:1
授权会员     
      

发表于 2008-7-12 17:38 
PARENT        CHILD        LEVEL1        RN        RN1
1        A        B        1        *3        3
2        B        C        2        *3*1.5        4.5
3        C        D        3        *3*1.5*1.2        5.4
4        D        E        4        *3*1.5*1.2*2        10.8
5        A        Z        1        *3        3


__________________
msn:anobluee@hotmail.com找兼职 最好是oracle开发 有时间来做9i Performance tuning|database administration fundamentals
只看该作者    顶部
在线/呼叫 jvkojvko
小马哥


精华贴数 1
个人空间 0
技术积分 5598 (236)
社区积分 44497 (17)
注册日期 2007-9-10
论坛徽章:20
祖母绿萤石祖母绿紫水晶紫水晶红宝石
海蓝宝石紫水晶蓝锆石月度论坛发贴之星金色在线徽章 

发表于 2008-7-12 19:43 
mark先


__________________
马无夜草不肥,人无外财不富。

-------------------------------
长期高价出售奥运章
只看该作者    顶部
相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问