按月份获取数量,map集合如:1月=100,获取数字按月份升序排序

一、sqlserver 语句
按月份获取数量,map集合如:1月=100,获取数字按月份升序排序

 

 

    
二、serviceImpl逻辑

/** * 知识总量/新增量 * @param vo * @return */ public List<Integer> docCountSituation(KnowledgeSearchVo vo){ Map<String,Object> map = new HashMap<String,Object>(); List<Integer> addList=new ArrayList<Integer>(); String sql="select sum(case when datepart(month,createdate)=1 then 1 else 0 end) as '1月',\n" + " sum(case when datepart(month,createdate)=2 then 1 else 0 end) as '2月',\n" + " sum(case when datepart(month,createdate)=3 then 1 else 0 end) as '3月',\n" + " sum(case when datepart(month,createdate)=4 then 1 else 0 end) as '4月',\n" + " sum(case when datepart(month,createdate)=5 then 1 else 0 end) as '5月',\n" + " sum(case when datepart(month,createdate)=6 then 1 else 0 end) as '6月',\n" + " sum(case when datepart(month,createdate)=7 then 1 else 0 end) as '7月',\n" + " sum(case when datepart(month,createdate)=8 then 1 else 0 end) as '8月',\n" + " sum(case when datepart(month,createdate)=9 then 1 else 0 end) as '9月',\n" + " sum(case when datepart(month,createdate)=10 then 1 else 0 end) as '10月',\n" + " sum(case when datepart(month,createdate)=11 then 1 else 0 end) as '11月',\n" + " sum(case when datepart(month,createdate)=12 then 1 else 0 end) as '12月'\n" + " from docbase\n" + " where isdelete=0 and docstatus=1"; if(vo!=null&&StringUtils.isNotEmpty(vo.getYear())){ sql+=" and datepart(year,createdate)=?"; map = dataService.getMap(sql,vo.getYear()); }else { map = dataService.getMap(sql); } List<Map.Entry<String,Object>> list = new ArrayList<Map.Entry<String,Object>>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, Object>>() { @Override public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) { return NumberHelper.string2Int(o1.getKey().replace("月",""),0)- NumberHelper.string2Int(o2.getKey().replace("月",""),0); } }); for(Map.Entry<String,Object> m: list){ addList.add(NumberHelper.string2Int(map.get(m.getKey()),0)); } return addList; }

 

上一篇:sqlserver 截取日期年份和月份


下一篇:mysql – 错误:DATEPART不存在,frp,SQL选择上个月的记录