如何根据特定条件延长日期

how to extend date by specific condition

查询将 valid_till 日期延长一个月的推荐更多的租户 超过两倍

示例数据:

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 推荐两次以上的 valid_till 日期。要获得谁推荐超过 2 次,我使用此代码:

> select referrer_id
>         from Referrals group by referrer_id having count(referrer_id)>2

但是如何为 valid_till 列中的 referrer_id >2 添加一个月。我相信 dateadd 将被使用,但我不知道如何使用。

试试这个

> 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 是您要添加一个月的日期,因为您没有指定

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
    )

我认为您正在寻找这种方法:

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