I have a table
Referrals
ref_id
referrer_id(same as profile id)
referrer_bonus_amount
referral_valid
valid_from
valid_till
valid_till
profile_id
referrer_id
select DATEADD(mm, 1, valid_till)
from Referrals
select
[referrer_id(same as profile id)],
count([referrer_id(same as profile id)])
from referrals
group by [referrer_id(same as profile id)]
having count([referrer_id(same as profile id)]) > 2
You could use the in
operator:
SELECT DATEADD(MM, 1, valid_till)
FROM referrals
WHERE referrer_id IN (SELECT referrer_id
FROM referrals
GROUP BY referrer_id
HAVING COUNT(*) > 2)
Or if you really need to update the table and not just query the added month, you could use the same idea in an update
statement:
UPDATE referrals
SET valid_till = DATEADD(MM, 1, valid_till)
WHERE referrer_id IN (SELECT referrer_id
FROM referrals
GROUP BY referrer_id
HAVING COUNT(*) > 2)