我目前有些代码根据行号设置日期。我想使用邮政编码设置所以邮政编码是相同的,它使用1月的第一个日期,当它到达1月30日它循环回到使用第一个为下一组邮政编码。
数据应如下所示:
CardNumber | PostCode | SchedServiceStart
34198 | GL53 | 01/01/2014
34199 | GL53 | 01/01/2014
35172 | GU4 | 02/01/2014
38764 | GU4 | 02/01/2014
83412 | GU4 | 02/01/2014
28171 | PL1 | 03/01/2014
28170 | PL1 | 03/01/2014
以下是当前使用行号设置日期的代码:
WITH CTE as
(SELECT CardNumber,
SchedServiceStart,
ROW_NUMBER() OVER (ORDER BY CardNumber) as rn
FROM tblContractSite
INNER JOIN tblContract ON tblContractSite.ContractRef = tblContract.ContractID
WHERE tblContractSite.SiteServiceTypeRef = 1 )
UPDATE CTE
SET SchedServiceStart
=CAST('01/01/2014' as Datetime)+(rn-1)%30 -- 30 day period in jan from the 1st
答案 0 :(得分:3)
在您的查询中,只需替换:
ROW_NUMBER() OVER (ORDER BY CardNumber) as rn
DENSE_RANK() OVER (ORDER BY POSTCODE) as rn
在这种情况下,您将根据rn
POSTCODE