Getting data for histogram plot

前端 未结 10 728
花落未央
花落未央 2020-11-29 15:27

Is there a way to specify bin sizes in MySQL? Right now, I am trying the following SQL query:

select total, count(total) from faults GROUP BY total;
<         


        
10条回答
  •  伪装坚强ぢ
    2020-11-29 16:07

    Ofri Raviv's answer is very close but incorrect. The count(*) will be 1 even if there are zero results in a histogram interval. The query needs to be modified to use a conditional sum:

    SELECT b.*, SUM(a.value IS NOT NULL) AS total FROM bins b
      LEFT JOIN a ON a.value BETWEEN b.min_value AND b.max_value
    GROUP BY b.min_value;
    

提交回复
热议问题