查询以将有效截止日期延长至已转介更多的租户一个月 两次以上
样本数据:
ref_id referrer_id referrer_bonus_amount referral_valid valid_from valid_till
263 5 2500 1 2015-07-05 2015-09-05
264 3 2500 1 2015-07-05 2015-09-05
265 5 1000 0 2015-12-13 2016-02-13
266 6 2500 0 2016-04-25 2016-06-24
267 10 1000 1 2015-07-01 2015-09-01
268 5 2500 1 2015-05-12 2015-07-12
269 10 2500 0 2015-08-05 2015-10-05
我想要的是增加已经引用两次以上的Referrer_id的有效截止日期。为了得到谁被推荐了两次以上,我使用了以下代码:
> select referrer_id
> from Referrals group by referrer_id having count(referrer_id)>2
但是如何在有效期至列中为Referrer_id> 2添加一个月。我相信可以使用dateadd,但是我不知道怎么做。
答案 0 :(得分:1)
UPDATE [YourTable]
SET [valid_till] = DATEADD(MONTH, 1, [valid_till])
WHERE [ref_id] IN (
SELECT [ref_id]
FROM [YourTable]
GROUP BY [ref_id]
HAVING COUNT([ref_id]) > 1
)
答案 1 :(得分:0)
尝试
> UPDATE TableName
set valid_till = DateAdd(month,1,valid_from)AS valid_till
where referrer_id in (select referrer_id
from Referrals group by referrer_id having count(referrer_id)>2)
在这种情况下,我假设valid_from是您要添加一个月的日期,因为您没有指定
答案 2 :(得分:0)
我认为您正在寻找这种方法:
SELECT
ref_id
, referrer_id
, referrer_bonus_amount
, referral_valid
, valid_from
, DATEADD(MONTH, 1, valid_till) AS valid_till
FROM (
SELECT
ref_id
, referrer_id
, referrer_bonus_amount
, referral_valid
, valid_from
, valid_till
, ROW_NUMBER() OVER(PARTITION BY referrer_id ORDER BY referrer_id) AS RN
FROM @test
) D
WHERE
RN > 2