表1结构
   field1   field2  field3
   日期型   数值型  外部主键
表2结构
   field3
   主键
求:1.表1中所有两个日期之间(avg(field2)/月)<固定值的所有记录
    2.表一中所有两个日期之间无记录的所有记录

解决方案 »

  1.   

    wo call,为什么没人,人都跑了吗?
      

  2.   

    比如说
    表1:缴费日期     缴费金额  用户ID
         2002-01-08   50        1
         2002-01-10   100       1
         2002-01-08   50        2
         2002-01-10   100       2
         2002-02-08   50        1
         2002-02-10   100       1
         2002-03-08   50        1
         2002-03-10   100       1
         2002-04-08   50        1
         2002-04-10   100       1
         2002-04-08   50        2
         2002-04-10   100       2
    表2:
         用户ID
         1
         2
    求:1.2002年1月--5月平均交费<30元的所有用户
        2.2002年1月--5月有连续2个月未交费的用户
      

  3.   

    先假设你的“缴费日期”是字符型的,这样简单些。
    1、select * from 表2 where  用户ID in (select 用户ID from (select 用户ID,avg(缴费金额) from 表1 where 缴费日期>'2002-01-00' and 缴费日期<'2002-06' goup by 用户ID having avg(缴费金额)<30) as a)
    2、select substring(缴费日期,1,7),用户ID from 表1 where group by substring(缴费日期,1,7),用户ID
      

  4.   

    TO BAIDEYI:
    ------------
    我表达的可能不清楚
    应该是月平均交费额而你的是平均交费额
      

  5.   

    1.
    SELECT id
    FROM TABLE1
    GROUP BY id
    HAVING (SUM(data) > 30 * DATEDIFF(mm, MIN([date]), MAX([date])) + 30)2.
      

  6.   

    SELECT id
    FROM TABLE1
    WHERE (3 NOT IN
              (SELECT month
             FROM (SELECT MONTH([date]) AS [month], id
                     FROM TABLE1
                     GROUP BY id, [date]) b
             WHERE b.id = table1.id))
    GROUP BY id
    这个可以判断是否缴纳了3月份的数据。
    如果判断两个月的,多增加where部分的判断吧.and 4 not in  or 4not in and 5 not in...
    我也不知道有没有什么简单的办法。呵呵找找看先,有点变态
      

  7.   

    1.select u_id 
    from table2 
    where u_id in (
      select u_id   
      from table1   
      where 缴费日期 between '2002-01-01' and '2002-05-31'   
      group by u_id,datepart(mm,缴费日期)  
      having avg(缴费金额)>30    
    )
      

  8.   

    第一个问题:  
    CREATE PROCEDURE   test1     @begin_date char(10),   @end_date char(10),@money money  AS  
    create table  #temp1(a char(10),b money ,c int )
    insert #temp1
    select c,sum(b),count(substring(a,1,7))
    from  a
    where substring(a,1,7)>=@begin_date and substring(a,1,7)<=@end_date
    group by c
    select * 
    from  #temp1
    where b/c<=@money
    drop table #temp1
    GO
    exec   test1 '2005-01-01','2007-01-01',60
      

  9.   

    sorry  第一个  更正
    CREATE PROCEDURE   test1     @begin_date char(10),   @end_date char(10),@money money  AS  
    create table  #temp1(a char(10),b money ,c int )
    insert #temp1
    select aa,sum(bb),count(substring(aa,1,7))
    from  a
    where aa>=@begin_date and aa<=@end_date
    group by aa select  *
    from  #temp1
    where b/c<=@money drop table #temp1
    GO
    exec   test1 '2005-01-01','2007-01-01',100
      

  10.   

    我晕了。我想这应该可以查出来了吧。
    1、select * from 表2 where  用户ID in (select 用户ID from (select 用户ID,substring(缴费日期,1,7),avg(缴费金额) from 表1 where 缴费日期>'2002-01-00' and 缴费日期<'2002-06' goup by 用户ID,substring(缴费日期,1,7) having avg(缴费金额)<30) as a)