我有下一个名为BALL的表:
Id ...... Color ........... Month
1 ........ blue ............ October
2 ........ red ............. January
3 ........ green ........... September
4 ........ red ............. October
5 ........ red ............. March
6 ........ blue ............ March
7 ........ red ............. March
我想要做的查询是:逐月显示蓝色的平均颜色数。
所以我想要下一个结果:
October 0.5
January 0
September 0
March 0.33
我在思考像:
SELECT BALL.Month, avg(BALL.Color)
FROM BALL
WHERE BALL.Color = 'blue'
GROUP BY BALL.Month;
但它没有用,
答案 0 :(得分:4)
您可以使用此方法
SELECT BALL.Month, avg(BALL.Color='Blue')
FROM BALL
GROUP BY BALL.Month;
答案 1 :(得分:1)
SELECT MONTH,ROUND(AVG(color =' blue'),2)
来自balls
在哪里1
按月分组
答案 2 :(得分:0)
尝试此查询。这有点复杂。但是作品
select t1.month,ifnull(t2.occur/t1.tot,0) as percentage from
(select month, count(*) as tot from ball group by month) as t1
left join
(select month, count(*) as occur from ball where color="blue" group by month) as t2
on t1.month=t2.month
答案 3 :(得分:0)
select t1.month,t1.mon_cnt,t2.col_cnt from (select month,count(month) mon_cnt from tab1 group by month) t1 join (select month,count(month) col_cnt from tab1 where color='blue' group by month) t2 on (t1.month=t2.month);