开发者

How to calculate sum for selected values from database in java?

I have selected the values from db by using hibernate and i want to calculate the sum for specified columns. my table con开发者_开发知识库tains `

 Id(int) transportname (string)  idletime (string)
  1       xxx                     04:00
  1        yy                     05:00
  2       xxx                     09:01

I have selected it by using hibernate query. I want to calculate the sum for idle column for each id. How can i do that by using java.


You could read the results in a loop, maintaining a mapping of idle values by id, something like this:

Map<Integer,Integer> idleById = new HashMap<Integer,Integer>();
while (// results) {
  int id = // read from results
  int idle = // read from results
  Integer value = idleById.get(id);
  if value != null) {
     value = new Integer(value.intValue() + idle);
  } else {
     value = new Integer(idle);
  }
  idleById.put(id, value);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜