如果您有下面的选择语句,其中 PK 是主键:
select distinct dbo.DateAsInt( dateEntered) * 100 as PK,
recordDescription as Data
from MyTable
并且输出是这样的(为了清楚起见,第一个数字是间隔的):
PK Data
2010 01 01 00 New Years Day
2010 01 01 00 Make Resolutions
2010 01 01 00 Return Gifts
2010 02 14 00 Valentines day
2010 02 14 00 Buy flowers
你想输出这样的东西:
PK Data
2010 01 01 01 New Years Day
2010 01 01 02 Make Resolutions
2010 01 01 03 Return Gifts
2010 02 14 01 Valentines day
2010 02 14 02 Buy flowers
是不是可以让PK中的“00”在单选内有“身份”的数字效果?否则,您如何为该日期的每个找到的活动将数字加 1?
我在输入时已经在考虑尝试用 group by 尝试 Sum(case when ?? then 1 end) 之类的东西。
编辑:(下面由 JohnFX 提供的答案)
这是最终的答案:
select PK + row_number() over
(PARTITION BY eventid order by eventname) as PK,
recordDescription
from (select distinct -- Removes row counts of excluded rows)
dbo.DateAsInt( dateEntered) as PK,
recordDescription as Data
from MyTable) A
order by eventid, recordDescription