sql每隔三天更新一次
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql每隔三天更新一次相关的知识,希望对你有一定的参考价值。
嗨,我有一个临时结果表,其中包含到达日期和出发日期,我需要在到达和离开之间每隔三天更新一次。这是我的输出
我能想到的唯一方法是使用DATEADD为3的倍数,但是到达和离开之间的天数可能是几个月甚至几年,所以我需要每隔三天设置一次更新,无论多少天
UPDATE #Report SET "Service" = CASE
WHEN Date = DATEADD(DAY,3,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,6,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,9,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,12,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,15,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,18,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
WHEN Date = DATEADD(DAY,21,CAST(FLOOR(CAST(Arr AS money)) AS Datetime)) THEN 'Full Service'
...
etc
ELSE 'Restricted Service' END
谁能想到解决方案吗?
答案
我希望这样的事情:
UPDATE #Report
SET Service = (CASE WHEN DATEDIFF(day, Arr, Date) % 3 = 0
THEN 'Full Service'
ELSE 'Restricted Service'
END);
我无法理解money
在您的查询版本中的转换。
另一答案
我找到了一个解决方案:
在源表中添加了一个Rank
RANK() OVER (PARTITION BY RoomID ORDER BY Date) AS Ranking
Divided rank by 3
UPDATE #x SET Ranking2 = CAST(Ranking AS DECIMAL(20,2))/3
Update only where this is a whole number
UPDATE #Report SET "Service" =
CASE WHEN FS.Ranking2 = ROUND(FS.Ranking2, 0) THEN 'Full Service'
ELSE 'Restricted Service' END
如果有更好的方法,请告诉我
以上是关于sql每隔三天更新一次的主要内容,如果未能解决你的问题,请参考以下文章