我有一字段:数量。如:数量
                       2
                       3
                       10 
                       12
需要用sql最后显示出: 数量总和,小于10总和,大于10小于20总和,小于10的数量占百分比 。请问一下我的sql语句该怎么写?

解决方案 »

  1.   

    select L.SumNum, M.SumNumLower10,N.SumNumBetween10and20,M.SumNumLower10/100  from
    (select  sum(数量) SumNumALL 
     TableName a) L ,
    (select sum(数量) SumNumLower10 
     TableName a where a.数量<10 ) M,
    (select sum(数量) SumNumBetween10and20 
     TableName a where a.数量>10 and a.数量<20 ) N就是这样.
      

  2.   

    declare @Table table(aa int) 
    declare @Table1 table(数量总和 int,小于10总和 int,大于10小于20总和 int,小于10数量百分比 int)
    insert @Table select 2
    insert @Table select 3
    insert @Table select 10
    insert @Table select 12insert @Table1 select (select sum(aa) as 数量总和 from @Table),
                          (select sum(aa) as 小于10总和 from @Table where aa < 10),
                          (select sum(aa) as 大于10小于20总和 from @Table where aa > 10 and aa < 20),
    (
    select 
     ((select sum(aa) from @Table where aa < 10)*100/(select sum(aa) as 数量总和 from @Table))
    as 小于10数量百分比 )
    select * from @table1
    -----------------------结果---------------------------
    27   5 12 18---------------------------------------------
    不知道是不是你想要的