表 XFB  
序号     姓名       消费金额      消费时间
 1        张三        300         2007-11-10
2        李四        500         2007-11-2
3        张三        200          2007-11-7
4        王五        700          2007-12-3
5        李四        400          2007-12-7
6        王五        600          2007-12-7
如何写一个SQL 能统计出在某段时间之内并且消费总额又在某段范围内的人名单呢?
例如 想统计出从11月到12月之间消费总额大于200且小于1000的名单 列出的应该如下
     序号     姓名     消费金额
     1        张三        500
    2        李四        900

解决方案 »

  1.   

    select  identity(int ,1,1) as id_ ,姓名 , sum(消费金额)as 消费金额  into table_  
    from table where   消费时间 between *** and ***  Group by 姓名
    select * from table_
    drop table table_
      

  2.   

    1楼的是sql server的写法,自己加些约束条件就能满足要求了.
    如果是oracle的话就只好用一个语句完成,你可以用having...具体的使用自己上网查.不要只想拿现成的,对你没好处.
      

  3.   

    select name,sum(price) as totalmoeny
    from test
    where riqi between '2007-11-01' and '2007-12-31'
    group by name
    having sum(price)>200 and sum(price)<1000 
      

  4.   

    select 序号,姓名,sum(消费金额) as 总消费 from 表 where 日期 between '2007-11-01' and '2007-12-31' group by 姓名 having sum(消费金额)>200 and sum(消费金额)<1000 
      

  5.   

    select * from 表 where 日期>'2007-11-01'   and 日期< '2007-12-31'   and 消费金额> 200   and 消费金额 <1000   
      

  6.   

    select  
           姓名 ,sum(消费金额)as '消费金额' 
    from   t
    where  消费时间 between '2007-11-01' and '2007-12-31'
    having sum(消费金额)>200 and 消费金额<1000 
                 
      

  7.   


    select  
           姓名 ,sum(消费金额)as '消费金额' 
    from   t
    where  消费时间 between '2007-11-01' and '2007-12-31'
    group by 姓名
    having sum(消费金额)>200 and 消费金额<1000 
                 
      

  8.   

    其实说到底就是用了一个Group by的知识点.