--统计
select 价格段=cast(gid*10+1 as varchar)+'-'+cast((gid+1)*10 as varchar)
,数量
,百分比=cast(cast(数量*100.0/(select count(*) from 表) as decimal(20,2)) as varchar)
from(
select gid=(price-1)/10,数量=count(*)
from 表
group by (price-1)/10
)a

解决方案 »

  1.   

    select '21-30' as 价格段,count(*) as 数量,count(*)/(select count(*) from yourtable) as 百分比 from yourtable where price between 21 and 30 union all
    select '31-40' as 价格段,count(*) as 数量,count(*)/(select count(*) from yourtable) as 百分比 from yourtable where price between 31 and 40 union all.....
      

  2.   

    --测试--测试数据
    create table 表(type varchar(10),price int)
    insert 表 select '小护士1型',30
    union all select '小护士2型',23
    union all select '小护士3型',55
    union all select '小护士4型',28 
    union all select '小护士5型',47
    union all select '索肤特2型',33
    union all select '索肤特3型',53 
    union all select '索肤特4型',44
    go--统计
    select 价格段=cast(gid*10+1 as varchar)+'-'+cast((gid+1)*10 as varchar)
    ,数量
    ,百分比=cast(cast(数量*100.0/(select count(*) from 表) as decimal(20,2)) as varchar)
    from(
    select gid=(price-1)/10,数量=count(*)
    from 表
    group by (price-1)/10
    )a
    go--删除测试环境
    drop table 表/*--测试结果
    价格段       数量        百分比  
    ----------- ----------- --------
    21-30       3           37.50
    31-40       1           12.50
    41-50       2           25.00
    51-60       2           25.00(所影响的行数为 4 行)
    --*/