目标是在每个segment_code
地方标记 1 条记录,Mtg_Flag = 1
如果记录没有,Mtg_Flag = 1
则使用 标记该记录Mtg_Flag = 0
。代码会将所有记录标记Mtg_Flag = 1
为一个,但是一旦运行,您如何将剩余记录标记在哪里Mtg_Flag = 0
。我的示例有 153 个不同的segment_codes
,只有 146 个带有Mtg_Flag = 1
. 那么,我如何标记剩余的 7 个segment_codes
where Mtg_Flag = 0
。
代码:
With LiveSam as (
Select acct_id,Row_Number() Over(Partition By Segment_Code Order By NewID())
as RowNumber
From Table_tr
Where LiveSam is NULL and seedrecordindicator is null and HasMtg_Flag = '1')
Update Table_tr
Set LiveSam = 'Y'
From LiveSam L
Where Table_tr.acct_id = L.acct_id
and L.RowNumber < 2
and Table_tr.acct_id is not NULL