一张订单表,有订单时间,订单号,客户名,订单金额,订单号唯一,一个客户可能有一张或多种订单,
现在要计算当客户数达到1000时的总销售额,达到2000时的总销售额,3000,4000,以此类推,高手求解??

解决方案 »

  1.   


    SELECT TOP 程序传递过来的1000参数变量 sum(je) FROM (SELECT  sum(je) AS je FROM Table1 GROUP BY 客户名编号) a先合并一个客户多个定单,再取前1000个客户,
      

  2.   

    谢谢2位回复,这其实和程序无关,只是统计分析用,看看每增加1000人的销售增长趋势,所以需要SQL语句,
    因为用户数的增加肯定随着时间的增加,在目前这张表中,我只能根据订单时间来判断人数,
    select sum(金额) from order where 订单时间<'xxxx-xx-xx xx:xx:xx',当用户正好是第1000个人的时候,得到这个订单时间,随即计算该时间之前的所有销售额,即算出人数达到1000人时的销售额,但这个订单时间如何得到呢??
    上面这句SELECT TOP 程序传递过来的1000参数变量 sum(je) FROM (SELECT sum(je) AS je FROM Table1 GROUP BY 客户名编号) a
    如果我的客户编号存在,而且是自增的,那没问题,现在没这个编号,
      

  3.   

    如只是看看。
    没编号直接就select top 1000 from 表  最后一条1000条就是最后时间 就行了。
      

  4.   

    可以用累加值 通过时间排序
    select * from
    (
    select *,(select sum(je) as je from tb where 客户名编号=t.客户名编号 and 时间<=t.时间) as je from tb t
    )
    where
     je<=1000
      

  5.   


    declare @i int,@sql varchar(200)
    set @i=1000
    set @sql='select a.* from 
    (select top '+convert(char(10),@i)+' * from tb) a left 
    join
    (select top '+convert(char(10),@i-1)+' * from tb) b
    on a.订单号=b.a.订单号
    where b.a.订单号 is null'
    exec (@sql)
      

  6.   


    declare @i int,@sql varchar(200)
    set @i=1000
    set @sql='select a.* from 
    (select top '+convert(char(10),@i)+' * from tb) a left 
    join
    (select top '+convert(char(10),@i-1)+' * from tb) b
    on a.订单号=b.a.订单号
    where b.订单号 is null'
    exec (@sql)