PKが主キーである以下のselectステートメントがある場合:
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回の選択で「ID」番号の効果を持たせることは可能ですか?それ以外の場合、その日付で見つかったアクティビティごとに1ずつ番号を増やすにはどうすればよいでしょうか。
Sum(case when ?? then 1 end)のようなものをgroupbyで試すように入力するときにすでに考えています。
編集:(以下の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