如何使用MySQL计算一条记录的平均值

如何使用MySQL计算一条记录的平均值,第1张

计算一条记录的平均值实际上就是横向求平均值,可以参考下例:

select id, (col1+col2+ col3)/3 as avgVal from T1 where id=123

select tn.date,case when tn.type=a then avg(tn.value) end as 'a',case when tn.type=b then avg(tn.value) end as 'b' from table_name tn group by tn.date

简单的叙述了下。可能有漏考虑的勿怪

select start, end, avg(value) from (

select t2.start, t2.end, t1.value from t1 left join t2 on t1.key between t2.start and t2.end

) as x group by start,end

效果见下图


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5948567.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-09
下一篇 2023-03-09

发表评论

登录后才能评论

评论列表(0条)

保存