找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1841|回复: 1

[求助] pandas实现 sql 递归

3

主题

19

帖子

19

积分

贫民

积分
19
wxl3322335 发表于 2018-1-8 01:07:16 | 显示全部楼层 |阅读模式
大神们请问如下sql 代码可以用pandas 实现吗?
谢谢大神们了,自己研究还就没有结果:



with t as
(
select *,1 lvl from bom where 父件编码=@No
union all
select a.父件编码,a.子件编码,a.用量*b.用量,b.lvl+1 from bom a,t b where a.父件编码=b.子件编码
)
select 子件编码 as 编码,用量
from t a




完整的存储过程是这样:


If not object_id('extend_bom') is null

Drop PROCEDURE extend_bom
Go
If not object_id('f_getbom') is null
Drop function f_getbom
Go
create function f_getbom(@No varchar(10),@lvl int)
returns table
as
return
with t as
(
select *,1 lvl from bom where 父件编码=@No
union all
select a.父件编码,a.子件编码,a.用量*b.用量,b.lvl+1 from bom a,t b where a.父件编码=b.子件编码
)
select 子件编码 as 编码,用量
from t a
where lvl<=@lvl
and not exists(
select 1
from t
where lvl<=@lvl
and a.子件编码=父件编码)
go


谢谢大神们了!
回复

使用道具 举报

3

主题

19

帖子

19

积分

贫民

积分
19
wxl3322335  楼主| 发表于 2018-1-8 10:44:55 | 显示全部楼层
自己顶!
回复

使用道具 举报

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

本版积分规则

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