日期:2014-05-16  浏览次数:20751 次

如何统计特定范围内的记录条数?
我有一个表A,字段为id,price(价格)。

价格在0~10之间。

能用一条语句统计下0~3,3~6,6~10这几这范围内的记录的条数吗?

进一步,如果我想知道0~3的记录条数站总记录的百分几要怎么做呢?

------解决方案--------------------
SQL code
select 
    case 
    when price between 0 and 3 then 1
    when price between 3 and 6 then 2
    when price between 6 and 10 then 3
    end,
    count(*)
from 我有一个表A
group by     case 
    when price between 0 and 3 then 1
    when price between 3 and 6 then 2
    when price between 6 and 10 then 3
    end