开发者

COUNT(Col1) by partitioning Col2 values in equidistant intervals

MySQL

I have two columns. Col1 contains a list of IDs, Col2 contains a list of numbers (ranging between -100 and 200+). I want the COUNT(IDs) WHERE Col2 BETWEEN -100 AND -75, going all the way to the upper limit开发者_运维百科 in intervals of 25.

What is the most efficient way of doing this using MySQL (or Excel). Thanks.


You can use this:

select count(Col1) cnt
from ta
group by ceil(col2 / 25);

Think, it'll be shorter =)


SELECT SUM(CASE WHEN Col2 BETWEEN -100 AND -75 THEN 1 ELSE 0 END) AS Interval1,
       SUM(CASE WHEN Col2 BETWEEN -74 AND -50 THEN 1 ELSE 0 END) AS Interval2,
       ...
       SUM(CASE WHEN Col2 BETWEEN 176 AND 200 THEN 1 ELSE 0 END) AS Interval12
    FROM YourTable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜