select time, total, convert(varchar(2),round(100*total/sum(total)))+'%' from
(select time, sum(price) as total from table where time between '20040904' and '20040906' group by time) a
--未测试

解决方案 »

  1.   

    给你一个我提的问题看看,我用的可以的
    http://community.csdn.net/Expert/topic/3433/3433671.xml?temp=.6966211
      

  2.   

    不好意思,round少了个参数,而且我理解错误,我以为是汇总数据..
    你9月4日的记录有两个,为什么查询结果是这个
    2004-9-4 200   21%
      

  3.   

    哦,那个时间之段前面还有一个custID之段的,每条记录的CustId都不同的
      

  4.   

    select time,price=sun(price)/count(*)
    from 表
    group by time
    不知道不对
      

  5.   

    select time,price,per=sun(price)/count(*)
    from 表
    group by time
    不知道不对
      

  6.   

    --测试表建立
    create table tb1(custid int ,time datetime,   price int)
    insert tb1 select 1,    
    '2004-9-2'  , 100 union all select 2,    
    '2004-9-2'  , 100 union all select 2,    
    '2004-9-3'  , 150 union all select 2,    
    '2004-9-4' , 180 union all select 1,    
    '2004-9-4' , 200 union all select 1,    
    '2004-9-5' , 300 union all select 1,    
    '2004-9-6' , 400 union all select 1,    
    '2004-9-21'       ,  700 union all select 2,    
    '2004-9-21'       ,  1000
    --语句
    declare @sum int
    select time, sum(price) as total into #t
    from tb1 where custid=1 and time between '20040904' and '20040906' group by time
    select @sum=sum(total)
     from #t
    select time, total, convert(varchar(100),
    round(cast(total as decimal(18,2))/@sum,2)*100)+'%' as rate 
    from #t
    drop table #t
    --结果time                          total                  rate
                                                                                          2004-09-04 00:00:00.000                                200         22.0000000000000%
    2004-09-05 00:00:00.000                                300         33.0000000000000%
    2004-09-06 00:00:00.000                                400         44.0000000000000%
      

  7.   

    select time,price,convert(varchar(1),convert(money,price/sum(price))*100)+'%' from tab where time='2004-9-4'
      

  8.   

    declare @sumprice int
    select @sumprice=sum(price) from tb1
    select time,sum(price),cast(sum(price)*100/(@sumprice) as varchar)+'%' from tb1 
    group by time
    having time>'2004-09-03' and time<'2004-09-07'
    select @sumprice
    ----------------------------------------------
    2004-09-04 00:00:00.000 380 12%
    2004-09-05 00:00:00.000 300 9%
    2004-09-06 00:00:00.000 400 12%