首页 > 其他 > 详细

根据某字段分组,获取当前数据成功或失败各自的统计数

时间:2018-01-04 22:18:41      阅读:481      评论:0      收藏:0      [点我收藏+]

技术分享图片

1、单独获取失败或成功的数据,并根据areaCode分组很好写:

SELECT p2.areaCode,COUNT(1) FROM vrv_paw_rulestatus p2 WHERE executeResult=1 GROUP BY p2.areaCode

技术分享图片

2、如果要根据areaCode分组获取失败的和成功的统计,在一条数据里,就需要一定的技巧在里面:

SELECT a.code,
(SELECT COUNT(1) FROM vrv_paw_rulestatus p2 WHERE executeResult=1 and p2.areaCode = p.areaCode ) AS count_0,
(SELECT COUNT(1) FROM vrv_paw_rulestatus p2 WHERE executeResult=0 and p2.areaCode = p.areaCode ) AS count_1 
FROM vrv_paw_area a LEFT JOIN vrv_paw_rulestatus p ON p.areaCode=a.code 
where 1=1 
GROUP BY a.code

技术分享图片

  public List<Object[]> queryBaseLineGroup(String areaCode, String startIP, String endIP) {
        StringBuffer hql = new StringBuffer("SELECT a.code,"
                + "(SELECT COUNT(1)  FROM vrv_paw_rulestatus p2 WHERE executeResult=1 and p2.areaCode = p.areaCode ) AS count_0  "
                + "FROM vrv_paw_area a LEFT JOIN vrv_paw_rulestatus p ON p.areaCode=a.code  where 1=1 ");
        if (StringUtils.isNotBlank(areaCode)) {
            hql.append(" AND a.code =‘"+areaCode+"");
        }
        hql.append("GROUP BY a.code ");
                
        List<Object[]> list = getSession().createSQLQuery(hql.toString()).list();
        return list;
    }

 

根据某字段分组,获取当前数据成功或失败各自的统计数

原文:https://www.cnblogs.com/goloving/p/8194997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!