[英]SQL UPDATE SET one column to be equal to 0.000 when there are two identical values
So here is the problem i'm trying to update column distance, first to sum the distance for each ownerID and after when there is a record with the same id address the first record should memories thee distance and the second should be 0.000 所以这是我试图更新列距离的问题,首先要求和每个ownerID的距离,然后在有具有相同ID地址的记录时,第一个记录应该存储您的距离,而第二个记录应该存储0.000
This is the expected result 这是预期的结果
So far I did the first part that calculates the distance using this code 到目前为止,我已经完成了使用此代码计算距离的第一部分
UPDATE Action_Distance
SET [distance]=(SELECT sum([distance])
FROM Action a2
WHERE [name]='travel' and a2.ownerID = Action_Distance.ownerId
)
WHERE [name]='drive_through'
I don't understand how it should be done. 我不知道该怎么做。
Assuming that id_action
is unique for each row, then CASE
expression together with a NOT EXIST
subquery can be used in this way: 假设id_action
对于每一行都是唯一的,则可以通过这种方式使用CASE
表达式和NOT EXIST
子查询:
UPDATE Action_Distance A
SET [distance] = CASE
WHEN NOT EXISTS (SELECT 'anything'
FROM Action_Distance B
WHERE B.[name] = 'drive_through'
AND a.ownerId = b.ownerId
AND a.id_action > b.id_action)
THEN (SELECT SUM([distance])
FROM Action a2
WHERE [name] = 'travel'
AND a2.ownerID = a.ownerId)
ELSE 0
END
WHERE
[name] = 'drive_through'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.