lingting1995 2015-07-20 07:36 采纳率: 47.4%
浏览 1779
已采纳

怎么根据年份统计数据库中各列的总值

数据库里面只有时间字段(包括年月日),资产名称,资产数量,资产金额,学校名称,怎样
按年度统计统计每年学校的资产总数,总金额,会的能提点下么?,求指点,能告诉大概
的方法,思路都好,在线等,谢谢 (java)

  • 写回答

3条回答

  • danielinbiti 2015-07-20 08:05
    关注
     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);
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?