统计

   正常在工作中经常会遇到需要统计的需求,所以使用sql完成数据统计成了必不可缺少的一项,下面分享一些版
主自己的几个实用例子,希望对大家有所帮助。
   前俩个是比较简单的,如果只是为了看统计每年中各月份的数据的可以直接跳过看第三个。

单一统计

单一的统计只是简单的统计加了自己的条件,统计一定条件下的数据条数。
<select id="countInfo" parameterType="com.xxx.statistics.dto.RequestxxxDTO" resultType="java.lang.Integer">
        SELECT COUNT(Id)  FROM tableName
        <!--where标签中的条件根据自己需求来定-->
        <where>
            isDel = 1
             <!-- 这里是可以选择不同时间段 -->
            <if test="startTime !=null and endTime !=null">
                and (creatDate BETWEEN #{startTime} AND  #{endTime})
            </if>
        </where>
</select>

分组统计

统计每一项的个数,对这一项进行分组,版主这里是ReasonId 原因的id,大家根据自己的需求更改
<select id="countDowngradeReason" parameterType="com.xxx.statistics.dto.RequestxxxDTO" resultMap="ResultMap">
	SELECT ReasonId,COUNT(Id) as num  FROM tableName
     <!--where标签中的条件根据自己需求来定-->
     <where>
         downgrade = 1 and isDel = 1
         <!-- 这里是可以选择不同时间段 -->
         <if test="startTime !=null and endTime !=null">
             and (creatDate BETWEEN #{startTime} AND  #{endTime})
         </if>
     </where>
      <!-- 这里是统计每个原因所出现的次数,所以使用原因id来进行分组 -->
     group by ReasonId
 </select>

分组排序统计

在分组统计的基础上加入排序
<select id="countDowngradeReason" parameterType="com.xxx.statistics.dto.RequestxxxDTO" resultMap="ResultMap">
	SELECT ReasonId,COUNT(Id) as num  FROM tableName
     <!--where标签中的条件根据自己需求来定-->
     <where>
         downgrade = 1 and isDel = 1
         <!-- 这里是可以选择不同时间段 -->
         <if test="startTime !=null and endTime !=null">
             and (creatDate BETWEEN #{startTime} AND  #{endTime})
         </if>
     </where>
      <!-- 这里是统计每个原因所出现的次数,所以使用原因id来进行分组 -->
     group by ReasonId
     <!-- 降序排序  -->
     order by num  desc
 </select>
       

关联查询和分组排序统计

   统计每一项的个数的同时,查出所对应的一些信息,在上面分组 排序统计的基础上,添加关联查询,查出对应得
原因用于显示,同样大家根据自己所需更改
 <select id="countDowngradeReason" parameterType="com.xxx.statistics.dto.RequestxxxDTO" resultMap="ResultMap">
     select b.reasonType as type,a.num as typeNum from
     <!-- 这里是统计每个原因所出现的次数,所以使用原因id来进行分组 -->
     (SELECT ReasonId,COUNT(Id) as num  FROM tableName
      <!--where标签中的条件根据自己需求来定-->
      <where>
         downgrade = 1 and isDel = 1
         <!-- 这里是可以选择不同时间段 -->
         <if test="startTime !=null and endTime !=null">
             and (creatDate BETWEEN #{startTime} AND  #{endTime})
         </if>
      </where>
     group by ReasonId
     order by num  desc) a,
     <!--b表是类似的配置表要取出对应的原因,因为比较简单,所以就不做单独处理,直接在sql中关联查了-->
     (SELECT reasonNum, reasonType
     FROM tableName_b) b
     where a.ReasonId = b.reasonNum
 </select>

统计每月的数据

   统计每一年中各个月份得数据,这里不给大家整太多,主要是怎么对 2021-11-12 14:55:57 这样得时间格式
得数据,去统计每年各个月份得数据,主要用的就是 DATE_FORMAT(creatDate,'%Y-%m') 函数,然后根据处理
过的时间去分组统计,就可以取到需要的数据,其中'%Y-%m'格式也是可以根据自己需求继续更改的。
<select id="countMonthAll" parameterType="com.xxx.statistics.dto.RequestxxxDTO" resultMap="ResultMap">
        SELECT
        DATE_FORMAT(creatDate,'%Y-%m') as Date,
        COUNT(Id) as Count
        from
        tableName
        <where>
            isFinal = 1
            <if test="startTime !=null and endTime !=null">
                and (creatDate BETWEEN #{startTime} AND  #{endTime})
            </if>
        </where>
        group by
        Date
</select>

多分组条件统计SQL模板

大概需求:统计每年每月每个人每个项目的次数统计
select a.Date,a.user_id,a.项目, (部门id,公司ID,<需要哪一项加哪一项,但是哪一项不能影响前边的分组,要不会报错>) count(a.activity_id) counNum
from (select DATE_FORMAT(start_time,'%Y-%m') as Date,user_id,部门id,公司ID,项目,activity_id 
from 表) a
group by a.Date,a.user_id,a.项目

结尾语

   这里的统计可能没有完全符合大家的需要,如果有需要,可以私信,版主会持续更新,不是职业发稿的,只是想要
总结一些自己用到的,一方便自己查询,二分享出来希望能帮助到他人,写的不好请多谅解,谢谢观看!
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐