简体   繁体   English

有两个相同的值时,SQL UPDATE SET一列等于0.000

[英]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.

相关问题 更新列,其中两个值的总和等于SQL Server中的零 - Update Column where two values sum to equal zero in SQL Server 如何遍历一个表并在一个字段中查找具有相同值的相邻行并在SQL中有条件地更新另一列? - How to loop through a table and look for adjacent rows with identical values in one field and update another column conditionally in SQL? 一列的SQL集值等于同一表中另一列的值 - SQL set values of one column equal to values of another column in the same table SQL UPDATE SET 一列等于另一列引用的相关表中的值? - SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column? SQL将具有两个不同值的两个相等的列名称合并为一行 - SQL Merge two equal column names with two different values into one row 值等于设定值时列的总和 - SUM of a column when values equal set value SQL更新两个相同的表 - SQL Update Two Identical Tables 更新一张表中的值(其中一列的值不相等) - Update Values in One Table where value of one column is not equal 我如何从第一列中包含两个相同值但其余列中包含不同数据的表创建一个数据集 Oracle - How would I create one data set from a table that contains two identical values in the first column but different data in the remaining columns Oracle 是否有一个 sql 代码可以根据两列将一列设置为相等 - Is there an sql code to set a column equal something based on two columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM