壹佰网|ERP100 - 企业信息化知识门户

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2024|回复: 3

请教:查询一个BOM中最底层的料号及其汇总数量的SQL怎么编写?

[复制链接]
发表于 2009/1/19 15:32:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。如果您注册时有任何问题请联系客服QQ: 83569622  。

您需要 登录 才可以下载或查看,没有帐号?注册

x
本帖最后由 tsinvan 于 2009-1-20 16:38 编辑

BOM结构如下:
Item A  Quantity: 1PCS--Item B  Quantity 1PCS
                                 --Item D  Quantity 1PCS
                                 --Item C  Quantity 2PCS--Item B   Quantity 1PCS
                                                                    --Item D   Quantity 1PCS
                                                                    --Item E    Quantity 2PCS--Item B  Quantity 5PCS
                                                                                                        --Item D  Quantity 1PCS


与BOM对应的表名为bom(item01,qty01, item02,qty02),数据存储如下:
A    1    B    1
A    1    C    2
A    1    D    1
C    1    B    1
C    1    D    1
C    1    E    2
E    1    B    5
E    1    D    3

我想通过SQL代码实现如下结果:
Item              Quantity
A                     1
B                     23
D                     15

SQL代码如何编写?  还请大侠出手帮忙.
本人在此谢过了.
发表于 2009/1/19 21:29:23 | 显示全部楼层
一级一级向下查,合并,最后在分组求和
最好能把原数据一个例子贴出来
发表于 2009/4/11 11:18:36 | 显示全部楼层
--取巧之作
select top 1 'a','1' from bom
union
select top 1 'b','23' from bom
union
select top 1 'd','15' from bom
发表于 2009/4/11 11:19:07 | 显示全部楼层
--言归正传
SELECT TOP 1 item01, qty01
FROM bom
WHERE (item01 = 'A')

union all
select 'B',(t1.qty02 * t2.qty02 * t3.qty02 + T4.qty02 * T5.qty02 + T6.qty02) as qty01  from
(select qty02 from bom where item01 = 'E' and item02 = 'B') as T1,
(select qty02 from bom where item01 = 'C' and item02 = 'E') as T2,
(select qty02 from bom where item01 = 'A' and item02 = 'C') as T3,

(select qty02 from bom where item01 = 'C' and item02 = 'B') as T4,
(select qty02 from bom where item01 = 'A' and item02 = 'C') as T5,
(select qty02 from bom where item01 = 'A' and item02 = 'B') as T6

union all
select 'D',(t1.qty02 * t2.qty02 * t3.qty02 + T4.qty02 * T5.qty02 + T6.qty02) as qty01  from
(select qty02 from bom where item01 = 'E' and item02 = 'D') as T1,
(select qty02 from bom where item01 = 'C' and item02 = 'E') as T2,
(select qty02 from bom where item01 = 'A' and item02 = 'C') as T3,

(select qty02 from bom where item01 = 'C' and item02 = 'D') as T4,
(select qty02 from bom where item01 = 'A' and item02 = 'C') as T5,

(select qty02 from bom where item01 = 'A' and item02 = 'D') as T6
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|手机版|壹佰网 ERP100 ( 京ICP备19053597号-2 )

Copyright © 2005-2012 北京海之大网络技术有限责任公司 服务器托管由互联互通
手机:13911575376
网站技术点击发送消息给对方83569622   广告&合作 点击发送消息给对方27675401   点击发送消息给对方634043306   咨询及人才点击发送消息给对方138011526

GMT+8, 2025/11/29 07:03 , Processed in 0.014161 second(s), 14 queries , File On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表