[SQL ] 求多个区间的平均

楼主: lovebbw (非胖勿擾)   2022-03-15 13:43:49
数据库名称:MySQL
数据库版本:10
内容/问题描述:
时间 资料数值
1/1 100
1/2 200
1/3 300
1/4 50
1/5 100
请问我想要用一行指令算出
1/1~1/2的平均,1/3~1/4的平均,1/1~1/5的平均
请问该怎么用?
目前想到的只有分成三个select 然后union起来才能在一次搜寻内做到
作者: criky (2501-2)   2022-03-18 06:51:00
select avg(case when date between 1/1 and 1/2 then dataelse null end),avg (case when date between 1/3 and 1/4then sata else null end),avg(data) from table

Links booklink

Contact Us: admin [ a t ] ucptt.com