问题描述
- 怎么根据年份统计数据库中各列的总值
- 数据库里面只有时间字段(包括年月日)资产名称,资产数量,资产金额,学校名称,怎样
按年度统计统计每年学校的资产总数,总金额,会的能提点下么?,求指点,能告诉大概
的方法,思路都好,在线等,谢谢 (java)
解决方案
class Record{ private String dateStr = """"; private String schoolName = """"; private int money = 0; public Record(String dateStrString schoolNameint money){ this.dateStr = dateStr; this.schoolName = schoolName; this.money = money; } public String getYear() { return dateStr.split(""-"")[0]; } public String getSchoolName() { return schoolName; } public int getMoney() { return money; }}public class Test { public List<Record> init(){ List<Record> list = new ArrayList(); list.add(new Record(""2015-01-01""学校A""200)); list.add(new Record(""2015-02-01""学校A""300)); list.add(new Record(""2015-03-01""学校A""400)); list.add(new Record(""2015-01-01""学校B""200)); list.add(new Record(""2015-01-01""学校B""600)); list.add(new Record(""2015-02-01""学校B""800)); return list; } public HashMap<StringHashMap<StringInteger>> tongJi(List<Record> list){ HashMap<StringHashMap<StringInteger>> rstMap = new HashMap(); for(int i=0;i<list.size();i++){ Record rd = list.get(i); if(rstMap.get(rd.getYear())==null){ rstMap.put(rd.getYear()new HashMap()); } HashMap<StringInteger> schoolMap= rstMap.get(rd.getYear()); if(schoolMap.get(rd.getSchoolName())==null){ schoolMap.put(rd.getSchoolName()0); } Integer value = schoolMap.get(rd.getSchoolName()); value = value + rd.getMoney(); schoolMap.put(rd.getSchoolName()value); } return rstMap; } public void print(HashMap<StringHashMap<StringInteger>> rstMap){ Iterator iter1 = rstMap.keySet().iterator(); while(iter1.hasNext()){ String year = (String)iter1.next(); HashMap<StringInteger> schoolMap= rstMap.get(year); Iterator iter2 = schoolMap.keySet().iterator(); while(iter2.hasNext()){ String schoolName = (String)iter2.next(); int value = schoolMap.get(schoolName); System.out.println(""year=""+year+""school=""+schoolName + ""金额=""+value); } } } /** * @param args */ public static void main(String[] args) { Test t = new Test(); List<Record> list = t.init(); HashMap map = t.tongJi(list);//统计 t.print(map); }}
解决方案二:
select sum(资产金额) from 表 group by 学校名称年份(日期取年,数据库都有对应的函数)
解决方案三:
sql中的sum(字段名)就能实现这个功能
时间: 2024-10-27 11:38:00