我正在将来自第 3 方提供商的数据转换为内部 SQL Server 数据库。传入表的数据如下所示:
ID Element1 Element2 Element3
-------------------------------------
1 43582 1 7
2 46852 2 6
3 46852 4 4
4 47895 2 9
我有一个脚本执行一个
Insert into....
Select.....
From......
正确地从源表正确导入数据,但目标表中的 1 个元素除外。
当我运行脚本时,结果表如下所示:
ID Location LocationCounter Drawer1 Drawer2
----------------------------------------------------
1 43582 NULL 1 7
2 46852 NULL 2 6
3 46852 NULL 4 4
4 47895 NULL 2 9
我需要修改我的脚本以填充
LocationCounter
列。我可以将该值默认为 1,但在位置 46852 的情况下,插入的第一条记录需要具有 LocationCounter
值 1,插入的第二条记录需要具有 LocationCounter
值 2.
非常感谢任何关于如何实现这一点的建议。
添加评论作为答案...
使用
ROW_NUMBER()
按element1划分。
select id, element1 as location,
row_number() over (partition by element1 order by id) as LocationCounter,
your_other_columns
from your_staging_table