<select id="findSum" parameterType="java.lang.Integer" resultType="java.util.Map">
SELECT COUNT(1) AS moduleTocal,
SUM(IF(result='true',1,0)) AS modulePass,
<!--SUM(CASE WHEN result = 'true' THEN 1 ELSE 0 END) AS passtotal,-->
SUM(total) AS caseTocal,
SUM(passed) AS casePass,
SUM(line_total) ,
SUM(line_cover),
SUM(func_cover),
SUM(func_total)
FROM ft_build_info WHERE build_id = #{buildId}
</select>
其中第三行和第四行的代码作用一样,只是两种不同的函数。这就是按条件汇总数据的函数:
SUM(IF(result='true',1,0)) AS modulePass,
SUM(CASE WHEN result = 'true' THEN 1 ELSE 0 END) AS passtotal,
注意:sum()是一个函数,在取值的时候要用map取值。这两个函数挺重要的,建议理解掌握
SELECT
t.*,
CONCAT (ROUND (IFNULL((t.Pass * 100 / t.Total), 0),2),'%') As Rate <!--求百分比并且保留两位小数-->
from
(
SELECT
COALESCE( SUM(total),0 ) As Total, <!--这个意思是当total为null时返回0-->
COALESCE( SUM(passed),0 ) AS Pass,
COALESCE( SUM(error),0 ) AS Error
FROM ft_build_info fb
<where>
<if test="project != null and project.size !=0">
and fb.ft_project_name in
<foreach collection="project" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="version != null and version.size !=0">
and fb.ft_version in
<foreach collection="version" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="moduleName != null and moduleName.size !=0">
and fb.module_name in
<foreach collection="moduleName" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="type != null and type.length !=0">
and fb.flag in
<foreach collection="type" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="beginTime != null and beginTime != ''">
and <![CDATA[fb.begin_time >= #{beginTime}]]>
</if>
<if test="endTime != null and endTime != ''">
and <![CDATA[fb.end_time <= #{endTime}]]>
</if>
</where>
) t
字符串日期格式的比较大小:
SELECT
*
FROM
ft_build_info fb
WHERE fb.begin_time >= CONCAT(DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 0 DAY),'%Y-%m-%d'), ' 00:00:00')
AND fb.end_time <= CONCAT(DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 0 DAY),'%Y-%m-%d'),' 23:59:59')
其中DATE_ADD()函数是在该日期上加一个时间数,INTERVAL 后为正数就是加,为负数是减。
DATE_SUB()函数是在该日期上减去一个时间数,INTERVAL 后为负数就是加,为正数是减。
在做求和并且计算百分比的时候出现了一个问题,记录一下!!
修改之前的sql:
SELECT
t.* ,
CONCAT (ROUND (IFNULL((t.LineCover * 100 / t.LineTotal), 0.00),2),'%' ) As LineRate
FROM
(
SELECT
COALESCE( SUM(fb.line_total),0 ) As LineTotal,
COALESCE( SUM(fb.line_cover),0 ) AS LineCover,
CONCAT(fb.module_name,'') AS moduleName
FROM
ft_build_info fb
WHERE fb.flag = 1 GROUP BY fb.module_name
) t
WHERE 1=1 ORDER BY LineRate desc LIMIT 10
结果如下:
很奇怪,我明明是按照LineRate降序排列 的,为什么第一条数据排在第一条,很明显不对的。这是因为我们在使用concat函数时拼接了一个‘%’,这样在后续使用ORDER BY LineRate的时候,就会以字符串形式进行排序,怎么解决呢?
方法如下 :
方法一:ORDER BY '123'+0;(首推)
方法二:ORDER BY '123'*1;
方法三:ORDER BY CAST('123' AS SIGNED);
方法四:ORDER BY CONVERT('123',SIGNED);
注意:其中'123'是你要排序的字段。
演示第一种方法:
————————————————
版权声明:本文为CSDN博主「geng2568」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/geng2568/article/details/89329444
原文链接:https://blog.csdn.net/geng2568/article/details/89329444