解决方案 »

  1.   

    看上去像是是递归CTE的事情,上测试数据和预期结果
      

  2.   

    bom   fsyscode csyscode q
               0001      1001       1
               0001      1002       10
               0001       1004     6
               0002       1001      2
               0002        1005     4
    inventory  syscode code  name 
                     0001     cp001   成品a
                      0002     cp002  成品b
                      1001     cl001    材料a
                      1002      cl002     材料b
                      1004      cl003    材料C
                      1005      cl004    材料d
    通过cl001查bom得结果为
       父项编码  父项名称  子项编码  子项名称  用量
       cp001         成品a       cl001       材料a       1
       cp002          成品b      cl001       材料a       2
    通过cl004查bom得结果为
      父项编码  父项名称  子项编码  子项名称  用量
       cp002       成品b      cl004         材料d    4
      

  3.   

    看看这个行不行
    with bom(fsyscode,csyscode,q)as(
    select  '0001','1001',1
    union all select '0001','1002',10
    union all select '0001','1004',6
    union all select '0002','1001',2
    union all select '0002','1005',4
    ),inventory(syscode,code,name)as(
    select '0001','cp001','成品a'
    union all select '0002','cp002','成品b'
    union all select '1001','cl001','材料a'
    union all select '1002','cl002','材料b'
    union all select '1004','cl003','材料C'
    union all select '1005','cl004',' 材料d'
    )
    select c.code,c.name,b.code,b.name,a.q
    from bom a,inventory b,inventory c
    where b.code='cl004'
    and a.csyscode=b.syscode
    and a.fsyscode=c.syscode
      

  4.   

    select i1.code,i1.name,i2.code,i2.name,b.q
    from bom b
      join inventory i1 on b.fsyscode=i1.syscode
      join inventory i2 on b.csyscode=i2.syscode
    where i2.code='cl004'