2012-05-08 46 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(); 

    ... 
}