[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