我目前正在尝试创建一个java程序:
我遇到了No.3的麻烦 - 我不知道如何根据另一栏的变化计算总数。
充实3号...... 有7列,1~3个字符串代码,用于部门,商店和区域ID(字符串); 4是个人的ID(String); 5~7个值为三种不同的支付工资,加班和调整后的总工资(BigDecimal)。 我需要添加并显示每个部门,商店和区域的总数,因为它们会发生变化(因为每个部门的字符串值都会发生变化):例如,程序会按顺序显示每一行的个人,然后显示部门的部门代码更改时的总计。当部门和商店都发生变化时,会显示这两个总数,一行接一行。
我曾想过使用treeMap,但我无法理解是否可以根据其他列的值进行排序;我想不出办法检测部门,商店和地区ID代码的变化......
我承认,这有点过头了。我将不胜感激任何帮助。 如果您需要更多信息,请告诉我 - 我觉得我已经描述了我需要的东西,但我可能错过了一些东西。
提前致谢!
答案 0 :(得分:1)
看一下甲骨文的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值
答案 1 :(得分:0)
我认为你不需要用Java接近3。您应该能够使用带有group by子句的sql“aggregate function”来获取您要查找的列的总和(总和)。
请参阅汇总函数sum和group by子句,或查看维基百科有关“aggregate functions”的文章。