一个sql题
想得到如下结果,sql语句怎么写
------解决方案--------------------SELECT 1 季度,AVG(产值) '平均值'
FROM TB
WHERE 月份 BETWEEN 1 AND 3
UNION ALL
SELECT 2 季度,AVG(产值) '平均值'
FROM TB
WHERE 月份 BETWEEN 4 AND 6
UNION ALL
SELECT 3 季度,AVG(产值) '平均值'
FROM TB
WHERE 月份 BETWEEN 7 AND 9
UNION ALL
SELECT 4 季度,AVG(产值) '平均值'
FROM TB
WHERE 月份 BETWEEN 10 AND 12
------解决方案--------------------select 季度,avg(产值) as 平均值
from (
select case when 月份 between 1 and 3 then 1 when 月份 between 4 and 6 then 2 when 月份 between 7 and 9 then 3 else 4 end 季度 ,产值 from tablename ) as a
group by 季度
------解决方案--------------------select q as 季度,avg(产值) as 平均值 from(
select (月份-1)/3+1 as q,产值 from tb
)t group by q