如何在Java中显示按结果集按日期分组的信息?

时间:2014-06-02 20:06:40

标签: java collections

作为Hibernate的结果集,我得到按日期和statusID分组的数据:

+-----+------------+--------+
|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 |
+-----+------------+--------+

使用迭代器我将获取每列的信息:

                Iterator it = resultSet.iterator();
            if (!it.hasNext()) {
                System.out.println("No any data!");
            } else {
                while (it.hasNext()) {
                    Object[] row = (Object[]) it.next();
                    long count = (Long) row[0];
                    Date date = (Date) row[1];
                    int statusID = (Integer) row[2];
                    }

最后我需要以这种方式显示它(每个日期显示每个id和小计信息的所有计数):

----------------------------------------------------------------------------------------------- 
    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
 ----------------------------------------------------------------------------------------------

我实际上该怎么做?

1 个答案:

答案 0 :(得分:0)

您必须按日期订购数据,然后在迭代时记住“组” - 即月份 - 你是。

在伪代码中:

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);