case - How to use having condition in SQL query -


select      userid,     case         when (count(case                         when onlinesportsgamewagers != 0                          1                          else null                    end)                + count(case                          when depositmade_amt != 0                            1                            else null                      end)) >= 10               "vip"             else "non-vip"     end vipcheck                   player_activity      userid = 2023410  group      year(txndate), month(txndate) 

this query determines user's vip status each month.

ultimately, want have query determines if user achieved vip status @ least 3 months (including current month). time being, it's user 2023410, want run whole database.

therefore ultimate output be:

user - vipcheck (3 different months w/ active status)

(one row per userid)

having count(case when (count(case when onlinesportsgamewagers != 0             1                 else null             end)                   + count(case when depositmade_amt != 0                         1                             else null                         end)) >= 10               1                 else 0                 end)  

tried above having statement, didn't work. suggestions?

if understand correctly, gets vip status 1 user month:

select userid, year(txndate), month(txndate),                                                                                                 (case when sum(case when onlinesportsgamewagers <> 0 1 else 0 end) +                   sum(case when depositmade_amt <> 0 1 else 0 end) >= 10              'vip'              else 'non-vip'         end) vipcheck              player_activity group userid, year(txndate), month(txndate); 

another aggregation want:

select userid,        (case when sum(vipcheck = 'vip') >= 3 'super-vip'              when sum(vipcheck = 'vip') >= 1 'vip'              else 'hoi polloi'         end) status (select userid, year(txndate), month(txndate),                                                                                                       (case when sum(case when onlinesportsgamewagers <> 0 1 else 0 end) +                         sum(case when depositmade_amt <> 0 1 else 0 end) >= 10                    'vip'                    else 'non-vip'               end) vipcheck                    player_activity       group userid, year(txndate), month(txndate)      ) uym group userid; 

Comments