ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

查询一年12个月的数据

2022-01-05 17:00:15  阅读:246  来源: 互联网

标签:datepart month sum when 查询 12 一年 else createdate


    /**
     * 知识累计量
     * @param vo
     * @return
     */
    public List<Integer> getCumulations(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);
            }
        });
        List<Integer> lists=new ArrayList<>();
        int sum=0;

            for(Map.Entry<String,Object> m: list){
                addList.add(NumberHelper.string2Int(map.get(m.getKey()),0));
            }
            for (Integer i:addList){
                sum=sum+i.intValue();
                lists.add(sum);
            }
            return lists;


    }

 

标签:datepart,month,sum,when,查询,12,一年,else,createdate
来源: https://www.cnblogs.com/jk200/p/15767782.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有