1、MySql 如何返回 时间差的平均值
//返回平均值为小时
SELECT AVG(TIMESTAMPDIFF(HOUR,submitTime,editTime)) as timediff from orders where editTime is not null;
//返回平均值为分钟
SELECT AVG(TIMESTAMPDIFF(MINUTE,submitTime,editTime)) as timediff from orders where editTime is not null;
//返回平均值为秒
SELECT AVG(TIMESTAMPDIFF(SECOND,submitTime,editTime)) as timediff from orders where editTime is not null;
https://blog.csdn.net/xzy565143480/article/details/90550871
2、MySql以一定的时间间隔统计
func (i *FlowInstanceBuidler) GetTrendChartByInterval(intervalType string, limit float64, whereCond string,fieldName string) (flowCountInfos []FlowTrendCount) {
var sql string
var fromSql string
fieldSql := "flow_id,count(*) as nums"
if whereCond != "" {
whereCond += fmt.Sprintf("and DATE_SUB(CURDATE(), INTERVAL %d %s) <= date(%s) ", int(limit), intervalType,fieldName)
} else {
whereCond = fmt.Sprintf("where DATE_SUB(CURDATE(), INTERVAL %d %s) <= date(%s) ", int(limit), intervalType,fieldName)
}
fromSql = fmt.Sprintf("FROM flow_instance %s GROUP BY internal,flow_id ORDER BY internal DESC ", whereCond)
switch intervalType {
case "day":
sql = fmt.Sprintf("SELECT %s, date_format(%s, '%%Y%%m%%d') internal %s ;", fieldSql,fieldName,fromSql)
case "week":
sql = fmt.Sprintf("SELECT %s, date_format(%s, '%%x%%v') internal %s ", fieldSql,fieldName,fromSql