gpt4 book ai didi

c# - 更新空行,该行没有任何唯一信息

转载 作者:太空宇宙 更新时间:2023-11-03 22:58:49 24 4
gpt4 key购买 nike

我有以下数据:

[Receipt]   [Date]     [Barcode]    [Quantity]  [Supplier]  [DeliveryID]
0001 01/01/2000 0000001 5 Grocery NULL
0001 01/01/2000 0000002 7 Grocery NULL
0001 01/01/2000 0000571 2 Grocery NULL
0002 01/01/2000 0000041 5 Grocey NULL
0002 01/01/2000 0000701 10 Grocey NULL
0003 01/01/2000 0000001 9 Groceri NULL

What can I do to put an incrementing value to the DeliveryID?

如您所见,没有任何唯一性可用于区分一行与另一行。很可能会再次输入类似于这些行中任何一行的类似数据。

不幸的是,我无法删除表并为新列创建一个新表。

我尝试先计算所有 null 行,

SELECT COUNT(*) as TotalCount FROM dbo.Delivery WHERE DeliveryID IS NULL

并创建一个 for 循环来更新。

但我意识到在第一个循环之后,所有的null 都会被替换为1,而不是每次循环更新每一行。

我曾想过将ReceiptSupplier合并成一个唯一的值,但正如我前面所说,有可能输入类似的数据,因此创建重复行,失去行的唯一性

最佳答案

使用行号

with D as
(
select Receipt, Date, Barcode, DeliveryID, row_number() over(order by receipt , Date, Barcode) as rn
from delivery
)

update D
set DeliveryID = rn
where DeliveryID is null

您甚至可以按收据进行分区以在收据组内提供每行:

with D as
(
select Receipt, Date, Barcode, DeliveryID, row_number() over(partition by receipt order by Date, Barcode) as rn
from delivery
)

update D
set DeliveryID = rn
where DeliveryID is null

关于c# - 更新空行,该行没有任何唯一信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44109893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com