1. 程式人生 > >根據某字段分組,獲取當前數據成功或失敗各自的統計數

根據某字段分組,獲取當前數據成功或失敗各自的統計數

result body color src base post esql rule append

技術分享圖片

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

根據某字段分組,獲取當前數據成功或失敗各自的統計數