select count(pepole) from table1 a,table1 b
where max(money)<2000 and min(money)>1000
and year='2002'
and a.pepole=b.pepole

解决方案 »

  1.   

    楼上写得不对,我的意思是每个月用户消费金额都应该在此范围内,如果中间某个月的用户消费金额不在此范围内,例如:
    1月份a用户消费 1500元
    2月份a用户消费 200元
    3月份a用户消费 1500 元
    此种情况不应统计
    只有以下情况应统计
    1月份a用户消费 1500元
    2月份a用户消费 1800元
    3月份a用户消费 1500 元
      

  2.   

    select sum(decode(floor(col/1000),1,1,2,1,0)) from table1 a,table2 b where a.用户=b.用户
      

  3.   

    select a.pepole,a.m_max,b.m_min from
    (select pepole,max(cast(money as integer)) m_max
     from test 
    where nf='2002' and cast(yf as integer) between 1 and 3
    group by pepole) a,
    (select pepole,min(cast(money as integer)) m_min
     from test 
    where nf='2002' and cast(yf as integer) between 1 and 3
    group by pepole) b
    where a.pepole=b.pepole
    and a.m_max<2000 and b.m_min>1000
    我在db2环境下写的,通过了测试,你改一下。
    cast(money as integer)是把数字转换为integer类型,你可去掉。
      

  4.   

    select * from test
    --------------------------
    NF   YF PEPOLE     MONEY     
    ---- -- ---------- ----------
    2002 1  Wang       1500      
    2002 2  Wang       1600      
    2002 3  Wang       1700      
    2002 1  Li         1700      
    2002 2  Li         800       
    2002 3  Li         1200        6 条记录已选择。
    --------------------------
    楼上结果:
    PEPOLE     M_MAX       M_MIN      
    ---------- ----------- -----------
    Wang              1700        1500  1 条记录已选择。
      

  5.   

    浪子兄:突然想起来了,在a和b的条件中还要加上money is not null这个条件的,不然,当某个月的money is null的时候,也可以检索出max()和min()的,所以这应该不符合楼主的要求的。现在晕晕乎乎的,不知道对不对,搂住试试吧 :)
      

  6.   

    TO jiezhi(西域浪子) :难道只能通过关联进行过滤吗?如果此表中有80万用户,这种方法会导致数据库性能急剧下降的.