包含重复项的表需要批量更新

发布于 2025-01-11 14:53:27 字数 4660 浏览 0 评论 0原文

我有一个包含这样的数据的表

transactionCodecolumn2column3column4wCodeaCodecolumn7column8column9column10liNumber
79386362INVOICENULL1MZ6905772021-01-28NULL2021-01-2816
79386362INVOICENULL1MD1918072021-01-282021-01-2814
79386312发票1MZ3207712021-01-282021-01-2811
79386312发票17803A1122021-01-282021-01-2842
79385762发票18201A2162021-01-292021-01-2911
79385982发票1SP0462712021-01-292021-01-29114

我也有一个这样的脚本,它为我找到重复项

WITH cte
 AS (SELECT transactionid,
            aCode,
            liNumber,
            wCode,
            RN = Row_number()
                   OVER(
                     partition BY 
                        transactionid, 
                        aCode, 
                        liNumber,
                        wCode
                     ORDER BY 
                        transactionid)
     FROM   duplicates)
SELECT * FROM   cte
WHERE  RN > 1;  

运行该脚本时,显示的数据采用这样的格式..

transactionIDaCodeliNumberwCodeRN
1012751DISCOUNT912

然后我可以在重复项表中搜索该 aCode 或 transactionID 以查看有多少个。到目前为止,在我的重复表中,该脚本总共返回 34,791 行。请注意,具有相同 liNumber 的项目需要更改。

我的问题是,如何处理如此大量的数据?

例如,

事务 7938636 可能有 5 行。全部具有相同的 wCode 和相同的 aCode 但 liNumber 会以 1、2、3、4 等增量递增。当一行有相同的liNumber时;说 1 那么它被归类为重复项。然后,我需要更新该重复行以继续增量(从 6、7、8 等)。

这有道理吗?

I've got a table containing data like this

transactionCodecolumn2column3column4wCodeaCodecolumn7column8column9column10liNumber
79386362INVOICENULL1MZ6905772021-01-28NULL2021-01-2816
79386362INVOICENULL1MD1918072021-01-28NULL2021-01-2814
79386312INVOICENULL1MZ3207712021-01-28NULL2021-01-2811
79386312INVOICENULL17803A1122021-01-28NULL2021-01-2842
79385762INVOICENULL18201A2162021-01-29NULL2021-01-2911
79385982INVOICENULL1SP0462712021-01-29NULL2021-01-29114

I've also got a script like this which finds the duplicates for me

WITH cte
 AS (SELECT transactionid,
            aCode,
            liNumber,
            wCode,
            RN = Row_number()
                   OVER(
                     partition BY 
                        transactionid, 
                        aCode, 
                        liNumber,
                        wCode
                     ORDER BY 
                        transactionid)
     FROM   duplicates)
SELECT * FROM   cte
WHERE  RN > 1;  

When running that script the data shown is in a format like this..

transactionIDaCodeliNumberwCodeRN
1012751DISCOUNT912

I can then search for that aCode or transactionID in the duplicates table to see how many there are. So far in my duplicates table, that script returns a total of 34,791 rows. Note, items that have the same liNumber needs to be changed.

My ask is, how do I go about doing this with this large amount of data?

For example,

Transaction 7938636 might have 5 rows. All with the same wCode and the same aCode BUT the liNumber goes up in increments like 1, 2, 3, 4 ect. When a row has the same liNumber; say 1 then that is classed as a duplicate. I then need to update that duplicate row to continue the increments, from 6, 7 , 8 ect.

Does this make sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏半生如梦愿梦如真 2025-01-18 14:53:27

由于 liNumber 不是相同的序列(如(1,2,3,4...等)),因此您可以通过将 liNumber 的最大值与相应的行号相加来更新重复项,从而采用解决方法,如下所示。

declare @tbl table(id int, wCode int, aCode varchar(50), liNumber int)

insert into @tbl
values(7938636,1,'MZ690577',1)
,(7938636,1,'MZ690577',1)
,(7938636,1,'MZ690577',2)
,(7938636,1,'MZ690577',3)
,(7938636,1,'MZ690577',8)
,(7938636,1,'MZ690577',9)
,(7938636,1,'MZ690577',9)

declare @maxvalue int = (select max(linumber) from @tbl)

;with cte as
(
select *,ROW_NUMBER()over(partition by liNumber order by id,liNumber) partitionedrn
,@maxvalue + ROW_NUMBER()over(order by id,liNumber)maxx
from @tbl
)
update cte set liNumber = maxx
where partitionedrn > 1

select * from @tbl

注意:这只是一个示例数据,我没有考虑您的表格的全部内容。

Since the liNumber is not of identical sequence like (1,2,3,4...and so on) you can go with a workaround by updating the duplicates by adding the max of the liNumber with the corresponding rownumber as below.

declare @tbl table(id int, wCode int, aCode varchar(50), liNumber int)

insert into @tbl
values(7938636,1,'MZ690577',1)
,(7938636,1,'MZ690577',1)
,(7938636,1,'MZ690577',2)
,(7938636,1,'MZ690577',3)
,(7938636,1,'MZ690577',8)
,(7938636,1,'MZ690577',9)
,(7938636,1,'MZ690577',9)

declare @maxvalue int = (select max(linumber) from @tbl)

;with cte as
(
select *,ROW_NUMBER()over(partition by liNumber order by id,liNumber) partitionedrn
,@maxvalue + ROW_NUMBER()over(order by id,liNumber)maxx
from @tbl
)
update cte set liNumber = maxx
where partitionedrn > 1

select * from @tbl

Note: This is just a sample data and I did not consider your table to its entirety.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文