程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ant-怎麼根據年份統計數據庫中各列的總值

ant-怎麼根據年份統計數據庫中各列的總值

編輯:編程綜合問答
怎麼根據年份統計數據庫中各列的總值

數據庫裡面只有時間字段(包括年月日),資產名稱,資產數量,資產金額,學校名稱,怎樣
按年度統計統計每年學校的資產總數,總金額,會的能提點下麼?,求指點,能告訴大概
的方法,思路都好,在線等,謝謝 (java)

最佳回答:


 class Record{
    private String dateStr = "";
    private String schoolName = "";
    private int money = 0;
    public Record(String dateStr,String schoolName,int 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<String,HashMap<String,Integer>> tongJi(List<Record> list){
        HashMap<String,HashMap<String,Integer>> 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<String,Integer> 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<String,HashMap<String,Integer>> rstMap){
        Iterator  iter1 = rstMap.keySet().iterator();
        while(iter1.hasNext()){
            String year = (String)iter1.next();
            HashMap<String,Integer> 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);
    }

}
lingting1995
lingting1995
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved