0

我有一个包含重复和唯一 ID 的表。我想在所有交易费用中添加 10 美分,在重复的第一行订单中添加 10 美分,例如:

What i start with:
order# Fee    
123    5    
123    4    
111    3
122    5

what I should get: 

order# Fee     
123    5.1    --duplicates
123    4.0    --should not have 10 cents. 
111    3.1
122    5.1

我尝试使用下面的代码,但它会更新每个订单#

-- updates table adds 10 cents to the first order of each duplicate and every unique order
update ebaytemp
set [transaction fees] = [transaction fees] +.10
from (SELECT [order#] 
from ebaytemp
GROUP BY order#
HAVING COUNT(order#) > 1) as E
4

1 回答 1

0

如果您有一个datetimeortimestamp列来跟踪何时输入交易(您怎么能不呢?),您可以基于该列的“第一行”并使用:

UPDATE ebaytemp a
INNER JOIN
(
    SELECT ordernum, MIN(date_entered) AS minentered
    FROM ebaytemp
    GROUP BY ordernum
) b ON a.ordernum = b.ordernum AND a.date_entered = b.minentered
SET a.fee = a.fee + 0.10
于 2012-07-23T22:05:38.480 回答