sql
SELECT COUNT(字段),分组字段,SUM(字段),SUM(字段)
FROM 表 GROUP BY 分组字段
java
EntityWrapper<ProjectEntity> pp= new EntityWrapper<ProjectEntity>(); pp.eq("depcode", community); int proc = projectService.selectCount(pp); pp.setSqlSelect("COUNT(depcode) as pro01,SUM(budgetmoney) as pro01money,status,SUM(usemoney)AS pro01usemoney").groupBy("status"); List<Map<String,Object>> obj=projectService.selectMaps(pp); // 定义下标数组 String[] indexStrList = {"01","02","03","04","05"}; List<String> indexList=Arrays.asList(indexStrList); List<String> newIndexList = new ArrayList<>(indexList); JSONObject jsonObject = new JSONObject(); for(Map<String,Object> m : obj){ if(newIndexList.indexOf(m.get("status")) != -1 ){ // 对象的key String key = "pro" + m.get("status"); jsonObject.put(key,m.get("pro01")); jsonObject.put("pro" + m.get("status") + "money", m.get("pro01money")); jsonObject.put("pro" + m.get("status") + "usemoney", m.get("pro01usemoney")); newIndexList.remove(m.get("status")); } } for(String str : newIndexList){ jsonObject.put("pro" + str,0); jsonObject.put("pro" + str + "money", 0); jsonObject.put("pro" + str + "usemoney", 0); } maptos.put("pro",jsonObject);
原文:https://www.cnblogs.com/yanchaohui/p/10042280.html