一个表有价格、种类等几列,怎样用一条sql语句实现这样的操作,查询价格在<100、100—500、500—1000、>1000的种类数量,查询结果如下显示 
价格                 数量
<100               3 
100—500          4 
500—1000        9 
>1000             48

解决方案 »

  1.   

    设价格为price 
    select 价格,count(*) 数量 
    from( 
    select  
       case  
          when price <100 then ' <100' 
          when price>=100 and price <500 then '100-500' 
          when price>=500 and price <1000 then '500-1000' 
          else '>1000' 
       end   价格, 
       a.* 
    from a) 
    gtoup by 价格
      

  2.   

    select a.价格,sum(a.数量) 数量
    from
    (
    select
       case 
          when 价格<100 then '<100'
          when 价格>=100 and 价格<500 then '100-500'
          when 价格>=500 and 价格<1000 then '500-1000'
          else  '>1000'
       end 价格, tb1.数量
    from tb1
    ) a
    group by a.价格
      

  3.   


    select a.价格,sum(a.数量) 数量 ,a.种类
    from
    (
    select
       case 
          when 价格<100 then '<100'
          when 价格>=100 and 价格<500 then '100-500'
          when 价格>=500 and 价格<1000 then '500-1000'
          else  '>1000'
       end 价格, tb1.数量
    from tb1
    ) a
    group by a.价格,a.种类