2012-05-08 48 views
0

我无法找到解决问题的方案。我有4个表格:创建计算总数量的递归SQL语句

BomModule:该表格代表数据库中的一个模块。

CREATE TABLE "BOMMODULE" 
(
    "MODULEID" NUMBER(10,0) NOT NULL ENABLE, 
    "ISROOTMODULE"  NUMBER(1,0) NOT NULL ENABLE, 
    "MODULENAME"  VARCHAR2(255 CHAR), 
    ... 
) 

BomItem:此表表示叶 - 或在数据库中的项目。

CREATE TABLE "BOMITEM" 
(
    "ITEMID"  NUMBER(10,0) NOT NULL ENABLE, 
    ... 
) 

ModuleConnection:该表映射一个模块到另一个父模块。您可以定义属于特定父模块的子模块的数量。

CREATE TABLE "MODULECONNECTION" 
(
    "ID"  NUMBER(10,0) NOT NULL ENABLE, 
    "QUANTITY" NUMBER(10,0) NOT NULL ENABLE, 
    "SUBMODULE_MODULEID" NUMBER(10,0) NOT NULL ENABLE, 
    "PARENTMODULE_MODULEID" NUMBER(10,0) NOT NULL ENABLE, 
    ... 
) 

ItemModuleConnection: 此表将全部离开,项目的模块。此外,您可以定义一个模块的项目数量。

CREATE TABLE "ITEMMODULECONNECTION" 
(
    "ID"  NUMBER(10,0) NOT NULL ENABLE, 
    "QUANTITY" NUMBER(10,0) NOT NULL ENABLE, 
    "ITEMID" NUMBER(10,0), 
    "MODULEID" NUMBER(10,0), 
    ... 
) 

正如您从表格结构中看到的,物品和模块彼此连接并且具有不同的数量。由于这样的事实,这些连接是非常灵活的,我不能够创建一个SQL语句,会提供给我的总数量为一个项目:

select quantity from ...... where itemId = xy; 

SQL语句应该检查所有的数量从项目根模块和它们相乘:

2 x rootmodule (total 2) 
-- 1x submodule 1 (total 2) 
-- 2x submodule 2 (total 4) 
---- 5x item 1 (total 20) 
---- 6x item 2 (total 24) 

请帮我创建这个sql语句,非常感谢你的回答!

约束:
- 它必须是一个SQL语句(这是在Java应用程序中使用)
- 数据库是的Oracle 11g

+0

我直接使用Java试了一下(收藏),但是使用了很多资源。不幸的是我不知道如何在SQL中使用递归。 – doonot

+0

您发布的表格中存在cut'n'paste错误。我试图解决它,但我不确定它是否正确。请检查 – APC

+0

好的,thx,它现在已经修复! – doonot

回答

0

我能够通过使用Java而不是SQL来解决此问题。这是我的方法:

/** 
* This recursive functions interates through the whole tree and checks if there are items and modules. 
* As soon as an item is found, it is multiplied with the module amount. The result is saved in a HashMap. 
* This HashMap is being parsed in the end. 
*/ 
public HashMap<Integer, Integer> updateBom(HashMap<Integer, Integer> bom, BomModule module, int amount, BomHandling bh) { 
    if(bom == null) { 
     bom = new HashMap<Integer, Integer>(); 
    } 
    // get all items for this parent module 
    Collection<ItemModuleConnection> items = bh.getItems(module.getModuleId()); 
    Iterator<ItemModuleConnection> itemIterator = items.iterator(); 

    while(itemIterator.hasNext()) { 
     ItemModuleConnection con = itemIterator.next(); 

     int itemQuantity = con.getQuantity() * amount; 

     // if bom item already exists in bom list, get it and update quantity 
     Integer currentItemQuantity = new Integer(0); 
     if(bom.containsKey(new Integer(con.getItem().getItemId()))) { 
      currentItemQuantity = bom.get(con.getItem().getItemId()); 
      bom.remove(bom.get(con.getItem().getItemId())); 
     } 
     bom.put(con.getItem().getItemId(), currentItemQuantity + itemQuantity); 
    } 

    // get all modules for this parent module 
    Collection<ModuleConnection> modules = bh.getConnections(module.getModuleId()); 
    Iterator<ModuleConnection> moduleIterator = modules.iterator(); 

    // set the quantity of the module by multiplying it with the amount 
    while(moduleIterator.hasNext()) { 
     ModuleConnection moduleCon = moduleIterator.next(); 
     int moduleQuantity = moduleCon.getQuantity(); 
     updateBom(bom, moduleCon.getSubModule(), moduleQuantity * amount, bh); 
    } 
    return bom; 
} 

之前,我打印的单品,我称这种updateBom() - 函数,然后从HashMap中的值:

HashMap<Integer, Integer> bom = updateBom(null, bomModule, 1, bh); 
Iterator<Map.Entry<Integer, Integer>> entries = bom.entrySet().iterator(); 

while (entries.hasNext()) { 

    Map.Entry<Integer, Integer> bomEntry = entries.next(); 
    BomItem item = bh.getBomItem(bomEntry.getKey()); 
    Integer itemAmount = bomEntry.getValue(); 

    ... 
}