如何在Java中显示结果集中按日期分组的信息?
作为Hibernate的结果集,我将按日期和状态ID对数据进行分组: +-----+------------+--------+ |COUNT| DATE | ID | +-----+------------+--+------ | 7 | 2014-03-28 | 1 | | 20 | 2014-03-28 | 3 | | 18 | 2014-03-28 | 4 | | 13 | 2014-04-10 | 2 | | 11 | 2014-04-11 | 3 | | 2 | 2014-04-11 | 4 | | 10 | 2014-04-13 | 1 | | 12 | 2014-04-13 | 2 | | 21 | 2014-04-14 | 2 | | 7 | 2014-04-14 | 3 | +-----+------------+--------+ 最后,我需要以这种方式显示它(对于每个日期,显示每个id的所有计数和小计信息): ----------------------------------------------------------------------------------------------- 日期状态ID_1状态ID_2状态ID_3状态ID_4小计 ----------------------------------------------------------------------------------------------- 2014-03-28 7 0 20 18 45 ---------------------------------------------------------------------------------------------- 每月总计7 0 20 18 45 ---------------------------------------------------------------------------------------------- 2014-04-10 0 13 0 0 13 2014-04-11 0 0 11 2 13 2014-04-13 10 12 0 0 22 2014-04-14 0 21 7 0 28 ---------------------------------------------------------------------------------------------- 每月总计104618276 ---------------------------------------------------------------------------------------------- 总数17 46 38 20 121 ----------------------------------------------------------------------------------------------如何在Java中显示结果集中按日期分组的信息?,java,collections,Java,Collections,作为Hibernate的结果集,我将按日期和状态ID对数据进行分组: +-----+------------+--------+ |COUNT| DATE | ID | +-----+------------+--+------ | 7 | 2014-03-28 | 1 | | 20 | 2014-03-28 | 3 | | 18 | 2014-03-28 | 4 | | 13 | 2014-04-10 | 2 | | 11
实际上我是如何做到这一点的?您必须按日期对数据进行排序,然后在迭代时记住您所在的“组”(即月份) 在伪代码中:
MonthYear lastGroup = null;
Long sum = 0;
Long sumGrandTotal = 0;
for(Row row : rows) {
currentGroup = row.year + "-" - row.month;
if(lastGroup != null && lastGroup != currentGroup) {
// new group -> write out sum
sumGrandTotal += sum;
out("Total " + lastGroup + ": " + sum);
sum = 0;
lastGroup = currentGroup;
}
// write out row
sum += row.data;
out(row);
}
// finally write out grand total
out("TOTAL " + sumGrandTotal);
-----------------------------------------------------------------------------------------------
DATE Status ID_1 Status ID_2 Status ID_3 Status ID_4 Subtotals
-----------------------------------------------------------------------------------------------
2014-03-28 7 0 20 18 45
----------------------------------------------------------------------------------------------
Total per month 7 0 20 18 45
----------------------------------------------------------------------------------------------
2014-04-10 0 13 0 0 13
2014-04-11 0 0 11 2 13
2014-04-13 10 12 0 0 22
2014-04-14 0 21 7 0 28
----------------------------------------------------------------------------------------------
Total per month 10 46 18 2 76
----------------------------------------------------------------------------------------------
TOTAL 17 46 38 20 121
----------------------------------------------------------------------------------------------
MonthYear lastGroup = null;
Long sum = 0;
Long sumGrandTotal = 0;
for(Row row : rows) {
currentGroup = row.year + "-" - row.month;
if(lastGroup != null && lastGroup != currentGroup) {
// new group -> write out sum
sumGrandTotal += sum;
out("Total " + lastGroup + ": " + sum);
sum = 0;
lastGroup = currentGroup;
}
// write out row
sum += row.data;
out(row);
}
// finally write out grand total
out("TOTAL " + sumGrandTotal);