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;
}