数据格式
USER_ID,AMOUNT(交易金额),month_no(月份)现在需要完成如下查询:
如果用户在任意三个月中单月交易金额>=5000,则表示该用户为活跃用户。求大神解决一下。谢谢
如:随机抽取3个月,1月,4月,5月,如果该用户在这三个月中有一个月交易>=5000,则是活跃用户。

解决方案 »

  1.   

    select decode(count(*),0,‘活跃’,‘不活跃’)
    from t
    where month_no in(‘01’,‘04’,‘07’)
      

  2.   

    -- 调整一下
    select decode(count(*),0,‘不活跃’,‘活跃’)
    from t
    where month_no in(‘01’,‘04’,‘07’)
    and amount > 5000
      

  3.   

    select user_id,
           decode(count(case
                          when AMOUNT >= 5000 then
                           AMOUNT
                          else
                           null
                        end),
                  0,
                  '不活跃',
                  '活跃')
      from t
     where month_no in ('01', '04', '07')
     group by user_id
      

  4.   

    按提供的需求:如果用户在任意三个月中单月交易金额>=5000,则表示该用户为活跃用户
    应该理解为:如果用户不存在选取的三个月中交易金额都小于5000,则为活跃用户。也就是在指定的时间范围内(比如一年等)小于5000的月份不能超过2个月
    select case when count(*) >2 then '活跃' else ‘不活跃’ end
    from t
    where year_month between :1 and :2
    and amount < 5000 and userid=:3;