计算同比和环比增长率

Posted 何必怀念

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算同比和环比增长率相关的知识,希望对你有一定的参考价值。

      今天做的项目中,遇到一个计算同期环比和上期环比的计算并显示出来。这是用ssm框架

  代码示例:

  

@RequiresPermissions("analysis:analysis:smartSearch")
    @RequestMapping(value = "/analysis/analysis/smartSearch")
    public String smartSearch()
    {
        Page<ElectronicTax> page = new Page<ElectronicTax>(request, response);
        //page.setPageSize( 25 );
        
        if ( POST )
        {
            if ( !"".equals(request.getParameter("pageSize")) )
            {
                el.setPage(page);
            }
            Map<String, ArrayList<Map<String, Object>>> dataList = new HashMap<String, ArrayList<Map<String,Object>>>();
            Map<String, List<String>> keyOne = new HashMap<String, List<String>>();
            String transverse=null, munehisa=null;
            
            List<Map<String, Object>> list = companyService.getElectronicTaxDao().getSmartSearch(el);
            
            
            for( Map<String, Object> m : list )
            {
                if ( m != null )
                {
                    Iterator<Entry<String, Object>> itr = m.entrySet().iterator();
                    while( itr.hasNext() )
                    {
                        Entry<String, Object> ety = itr.next();
                        String key = ety.getKey();
                        String value = ety.getValue().toString();
                        
                        if ( "amount".equals(key) || ("accounting".equals(key) && el.getIsTime() == null )) continue;
                        
                        if ( keyOne.get(key) == null )
                        {
                            if ( munehisa == null )
                            {
                                munehisa = key;
                            }
                            else
                            {
                                transverse = key;
                            }
                            keyOne.put(key, new ArrayList<String>());
                        }
                        
                        if ( !keyOne.get(key).contains( value ) )
                        {
                            keyOne.get(key).add(value);
                        }
                    }
                }
            }
            //横向为空,则只选择了一个字段,那么横向TITLE为金额
            if ( transverse == null )
            {
                //transverse = "amount";
                keyOne.put(transverse, new ArrayList<String>());
                keyOne.get(transverse).add("金额");
                System.out.println(keyOne.get(transverse));
            }
            else
            {
                if ( keyOne.get(transverse).size() > keyOne.get(munehisa).size())
                {//纵横向互换
                    String s = transverse;
                    transverse = munehisa;
                    munehisa = s;
                }
                
                model.addAttribute("transverse_param", getParam(transverse));
            }
            
            model.addAttribute("munehisa_param", getParam(munehisa));
            Double currentsAmounts = null;
            Double LastsAmounts = null;
                    
            for( Map<String, Object> m : list )
            {
                if ( dataList.get( m.get(munehisa) ) == null )
                {
                    ArrayList<Map<String, Object>> tmpList = new ArrayList<Map<String, Object>>();
                    
                    Map<String, Object> twelveMap = new HashMap<String, Object>();
                    
                    for( Map<String, Object> mm : list )
                    {
                        if ( m.get(munehisa).equals( mm.get(munehisa) ) )
                        {
                            if ( transverse == null )
                            {
                                twelveMap.put("金额", mm.get("amount"));
                            }
                            else
                                twelveMap.put(mm.get(transverse).toString(), mm.get("amount"));
                        }
                    }
                    
                    for( String ym : keyOne.get(transverse) )
                    {
                        Map<String, Object> map = new HashMap<String, Object>();
                        
                        Double currentAmount;
                        map.put(transverse, ym);
                        if ( twelveMap.get(ym) == null )
                        {
                            currentAmount = 0.0;

                        }
                        else
                        {
                            currentAmount = Double.parseDouble( twelveMap.get(ym).toString() );
                        }
                        map.put("amount", currentAmount);
                        
                        //对比计算
                        if ( el.getIdenticalCompare() != null )
                        {
//                            System.out.println(m.get(transverse).toString());
//                            System.out.println(m.get(munehisa).toString());
//                            System.out.println(munehisa);
//                            Double amount = companyService.getLastYearCount(m.get(transverse).toString(), m.get(munehisa).toString(), munehisa);
//                           double amount=20;
                           Double amount = 30d;
                           amount.toString();
                            map.put("identical", currentAmount - amount);
                            
                            if ( amount > 0  )
                            {    
                                map.put("identicals", (currentAmount - amount)/amount);
                            }
                            else map.put("identicals", 100);
                        }
                        if ( el.getRingCompare() != null )
                        {
                            Double amount = companyService.getLastMonthCount(m.get(transverse).toString(), m.get(munehisa).toString(), munehisa);
                            if ( amount > 0  )
                            {    
                                map.put("ring", (currentAmount - amount)/amount);
                            }
                            else map.put("ring", 100);
                        }
                        
                        if ( el.getCumulativeCompare() != null )
                        {
                            Double currentAmounts = companyService.getCumulativeCompare(m.get(transverse).toString(), m.get(munehisa).toString(), munehisa, "current");
                            Double LastAmounts = companyService.getCumulativeCompare(m.get(transverse).toString(), m.get(munehisa).toString(), munehisa, "");
                            //所有行业同比增量
                            if ( currentsAmounts == null )
                            {
                                currentsAmounts = companyService.getCumulativeCompare(m.get(transverse).toString(),"", munehisa, "current");
                                LastsAmounts = companyService.getCumulativeCompare(m.get(transverse).toString(), "", munehisa, "");
                            }
                            
                            map.put("cumulative", currentAmounts);
                            map.put("cumulatives", currentAmounts-LastAmounts);//增量
                            
                            if ( LastAmounts > 0 )
                            {
                                map.put("cumulativess", (currentAmounts-LastAmounts)/LastAmounts);//增速
                            }
                            else
                            {
                                map.put("cumulativess", 100);//增量
                            }
                            
                            if ( currentsAmounts > 0 || LastsAmounts > 0)
                            {
                                map.put("cumulativesss", (currentAmounts-LastAmounts)/(currentsAmounts-LastsAmounts));//增速
                            }
                            else
                            {
                                map.put("cumulativesss", 100);//增量
                            }
                        }
                        
                        tmpList.add( map );
                    }
                    
                    dataList.put(m.get(munehisa).toString(), tmpList);
                }
            }
            
            model.addAttribute("dataList", dataList);
            model.addAttribute("transverse", keyOne.get(transverse));
        }
        
        if ( !"".equals(request.getParameter("pageSize")) )
        {
            model.addAttribute("page", page);
        }
        model.addAttribute("yearMonthList", companyService.getYearMonth());
        
        return "addons/analysis/analysis/smartSearch";
    }

 

以上是关于计算同比和环比增长率的主要内容,如果未能解决你的问题,请参考以下文章

请教Oracle计算同比和环比sql语句

Hive之同比环比的计算

实现同比环比计算的N种姿势

实现同比环比计算的N种姿势

数据分析SQL环比增长率同比增长率

数据分析SQL环比增长率同比增长率