1.写出连续三个月有费用产生的用户  
 账单表结构:  
 user_id   bill_month  amount  
 20050101  200801      100.32  
 20050102  200801      60.99  
............................  2. 写出在网日期(使用日期)三个月以上的用户  
   套餐表结构  
   user_id    apply_date   ......  
   20050101    2008-01-01  ......  
   20050102    2008-02-01  .......  
   ...............................

解决方案 »

  1.   

     select * from 
    (
     select user_id,bill_month month1,amount,
       lead(bill_month,1) over (order by user_id,bill_month) month2,
       lead(bill_month,2) over (order by user_id,bill_month) month3,
       lead(user_id,1) over (order by user_id,bill_month) user_id_2,
       lead(user_id,2) over (order by user_id,bill_month) user_id_3,
     from tablename 
     ) t  where user_id=user_id_2 and user_id=user_id_3  and
       addmonth(month1,1)=month2 and addmonth(month1,2)=month3;这个思想可以实现,后面判断月份的条件 得根据你的bill_month类型修改下;
      

  2.   

    select * from tablename where addmonth(apply_date,3)<sysdate
      

  3.   

    select *
      from (select t.user_id,
                   row_number over(partition by t.user_id order by t.bill_month desc) amount_cnt
              from table_name t
             where amount > 0) a
     where amount_cnt >= 3