查看: 10332|回复: 39

能解这个的,一定是SQL高手。

[复制链接]
论坛徽章:
3
ITPUB9周年纪念徽章
日期:2010-10-08 09:32:252013年新春福章
日期:2013-02-25 14:51:24优秀写手
日期:2014-11-29 06:00:13
跳转到指定楼层
1#
发表于 2008-7-12 11:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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语句来实现这个功能。

请大家帮忙想象办法。
论坛徽章:
468
2#
发表于 2008-7-12 12:26 | 只看该作者
BOM结构啊。。。

使用道具 举报

回复
论坛徽章:
5
生肖徽章2007版:鸡
日期:2008-01-02 17:35:53奥运会纪念徽章:羽毛球
日期:2008-06-23 12:00:05奥运会纪念徽章:柔道
日期:2008-07-04 09:42:36奥运会纪念徽章:皮划艇激流回旋
日期:2008-08-12 14:50:402010新春纪念徽章
日期:2010-03-01 11:19:07
3#
发表于 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

使用道具 举报

回复
招聘 : 数据库开发
论坛徽章:
0
4#
发表于 2008-7-12 14:01 | 只看该作者

使用道具 举报

回复
招聘 : 数据库管理员
论坛徽章:
2
2015年新春福章
日期:2015-03-04 14:51:122015年新春福章
日期:2015-03-06 11:57:31
5#
发表于 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

使用道具 举报

回复
论坛徽章:
33
劳斯莱斯
日期:2013-08-08 14:01:23三菱
日期:2013-09-28 10:16:06一汽
日期:2013-11-19 17:01:11凯迪拉克
日期:2013-12-07 17:11:282014年新春福章
日期:2014-02-18 16:42:02马上有房
日期:2014-02-18 16:42:02itpub13周年纪念徽章
日期:2014-09-27 14:20:21itpub13周年纪念徽章
日期:2014-10-08 15:13:38懒羊羊
日期:2015-03-04 14:52:112015年新春福章
日期:2015-03-06 11:58:18
6#
发表于 2008-7-12 14:50 | 只看该作者
如果层次不确定的话可能不好写

使用道具 举报

回复
论坛徽章:
3
ITPUB9周年纪念徽章
日期:2010-10-08 09:32:252013年新春福章
日期:2013-02-25 14:51:24优秀写手
日期:2014-11-29 06:00:13
7#
 楼主| 发表于 2008-7-12 17:00 | 只看该作者
借用楼上的话,层次多,且不确定的情况下,不好写。

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

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

原帖由 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

使用道具 举报

回复
论坛徽章:
3
授权会员
日期:2006-11-08 09:14:34ITPUB9周年纪念徽章
日期:2010-10-08 09:31:22优秀写手
日期:2014-05-20 05:58:48
8#
发表于 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;

使用道具 举报

回复
论坛徽章:
3
授权会员
日期:2006-11-08 09:14:34ITPUB9周年纪念徽章
日期:2010-10-08 09:31:22优秀写手
日期:2014-05-20 05:58:48
9#
发表于 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

使用道具 举报

回复
论坛徽章:
9607
土豪章
日期:2013-12-31 14:11:39土豪章
日期:2013-12-31 14:11:39阿森纳
日期:2013-06-03 17:00:31阿森纳
日期:2013-10-11 09:27:58法拉利
日期:2013-12-27 15:20:30林肯
日期:2013-12-27 15:19:09法拉利
日期:2013-12-27 15:20:30法拉利
日期:2013-12-27 15:20:30法拉利
日期:2013-12-27 15:20:30法拉利
日期:2013-12-27 15:20:30
10#
发表于 2008-7-12 19:43 | 只看该作者
mark先

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表