2013-10-16 58 views
0

目前我正在試圖創建一個Java程序:添加的總列時,另一列更改

  1. 從Oracle SQL數據庫(字符串和BigDecimal的值)獲取的數據,並將它們在ResultSet
  2. 顯示每行
  3. 在每個組的末尾添加一個「總計」部分,除以3列字符串值。

我遇到了第3號的麻煩 - 我不知道如何根據另一列中的更改來計算總計。

要充實3號... 有7列,1〜3部門,商店和區域ID的字符串代碼(字符串); 4是個人的ID(字符串); 5〜7三種不同付款的工資,加班和調整後總工資(BigDecimal)的值。 我需要將每個部門,商店和區域的總計加起來並顯示出來(因爲每個部門,商店和地區的字符串值都會發生變化):例如,程序會按順序顯示每行個人,然後顯示部門代碼更改時的總計。當部門和商店都發生變化時,這兩個總計都會顯示,一行接一行。

我想過使用treeMap,但我無法過去是否可以根據另一列的值進行排序;我無法想出一種方法來檢測部門,商店和區域ID代碼的變化...

我承認這有點過頭了,我會很感激任何幫助。 請讓我知道,如果你需要更多的信息 - 我覺得我已經描述了需要什麼,但我可能錯過了一些東西。

在此先感謝!

+0

是否可以爲您發佈您的架構創建腳本的操作碼? – axiopisty

+0

謝謝axiopisty和德文你的答案!我可以通過使用if-else梯形圖和打印/重置每個部門的總計計算方法來完成。商店和地區ID。儘管如此,我仍然在學習,所以我會根據功能來研究聚合和組合,以及您可以用它們做什麼。 Axiopisty,我認爲你的意思是我在代碼中使用的SQL函數? (模式創建腳本:我很抱歉,行話不太流利,所以我去了谷歌)我可以很快發佈;不幸的是我目前無法訪問代碼:/ –

回答

1

看看Oracle的Analytic Functions
例如:

with tab1 as (
     select 'Dept 1' dept, 'Store 1' store, 'Region 1' region, 'ASmith' id, 101 salary, 1 overtime, 10 adjusted from dual union all 
     select 'Dept 1' dept, 'Store 1' store, 'Region 1' region, 'BSmith' id, 102 salary, 2 overtime, 10 adjusted from dual union all 
     select 'Dept 1' dept, 'Store 2' store, 'Region 1' region, 'CSmith' id, 104 salary, 4 overtime, 10 adjusted from dual union all 
     select 'Dept 1' dept, 'Store 2' store, 'Region 1' region, 'DSmith' id, 108 salary, 8 overtime, 10 adjusted from dual union all 
     select 'Dept 1' dept, 'Store 3' store, 'Region 1' region, 'DSmith' id, 108 salary, 8 overtime, 10 adjusted from dual union all 
     select 'Dept 2' dept, 'Store 1' store, 'Region 1' region, 'DSmith' id, 108 salary, 8 overtime, 10 adjusted from dual union all 
     select 'Dept 2' dept, 'Store 1' store, 'Region 1' region, 'ESmith' id, 116 salary, 16 overtime, 10 adjusted from dual union all 
     select 'Dept 2' dept, 'Store 2' store, 'Region 1' region, 'FSmith' id, 132 salary, 32 overtime, 10 adjusted from dual union all 
     select 'Dept 2' dept, 'Store 2' store, 'Region 1' region, 'ESmith' id, 116 salary, 16 overtime, 10 adjusted from dual union all 
     select 'Dept 2' dept, 'Store 3' store, 'Region 1' region, 'FSmith' id, 132 salary, 32 overtime, 10 adjusted from dual 
    ) 
select dept, store, region, sum(salary), sum(overtime), sum(adjusted) 
    from tab1 
group by rollup(dept, store, region); 

生產:

DEPT STORE REGION SUM(SALARY) SUM(OVERTIME) SUM(ADJUSTED) 
------ ------- -------- ----------- ------------- ------------- 
Dept 1 Store 1 Region 1  203    3   20 
Dept 1 Store 1 **   203    3   20 
Dept 1 Store 2 Region 1  212   12   20 
Dept 1 Store 2 **   212   12   20 
Dept 1 Store 3 Region 1  108    8   10 
Dept 1 Store 3 **   108    8   10 
Dept 1 **  **   523   23   50 
Dept 2 Store 1 Region 1  224   24   20 
Dept 2 Store 1 **   224   24   20 
Dept 2 Store 2 Region 1  248   48   20 
Dept 2 Store 2 **   248   48   20 
Dept 2 Store 3 Region 1  132   32   10 
Dept 2 Store 3 **   132   32   10 
Dept 2 **  **   604   104   50 
**  **  **   1127  127   100 

其中**表示NULL值

0

我不認爲你需要用Java來處理3。你應該能夠使用一個帶有group by子句的sql「集合函數」來獲得你正在尋找的列的總和(總和)。

查看聚合函數sumgroup by子句或查看維基百科有關「aggregate functions」的文章。