【问题标题】:Is there a SQL update query to update the same value for multiple records within the same user id?是否有 SQL 更新查询来更新同一用户 ID 内的多条记录的相同值?
【发布时间】:2019-09-08 20:36:51
【问题描述】:

我有一列仅用于计算。它显示代表休假时间的 0 或正数。在我们的时间表中,它输入为负数,我将其转换为正数进行计算。对于员工的每个条目,我需要该正值进入 NonWorkHrs 列。对于一个用户 ID 中的所有记录,它必须是相同的值。

我尝试过使用 case 语句并在子查询中选择 max

update DME
set NonWorkHrs = 
(
 select max(VacationHours)
 from DME b
 where useruid = b.useruid
 and useruid in (1,2,3)
 and NonWorkHrsHolder > 0
)
where useruid in (1,2,3)

我也试过一个case语句

update DME
set NonWorkHrs =
(case 
  when (VacationHours > 0)
  then (VacationHours)
 --no else statement is need. All rows should have the same value
 end
 ) 
where useruid in (1,2,3)

我在 VacationHours 列中将负 TimeEntered 值转换为正值。 NonWorkHrs 列用于在计算中确定实际工作时间。

预期的结果是

Useruid UserFullName    NonWorkHrs  VacationHours   TimeEntered
1       Jane Doe         8             8             -8
1       Jane Doe         8             0              10
1       Jane Doe         8             0              12
2       John Doe         18            18            -18
2       John Doe         18            0              23
3       Bob Builder      16            16            -16
3       Bob Builder      16            0              40

实际结果是

Useruid UserFullName    NonWorkHrs  VacationHours   TimeEntered
1       Jane Doe          18           8             -8
1       Jane Doe          18           0              10
1       Jane Doe          18           0              12
2       John Doe          18           18            -18
2       John Doe          18           0              23
3       Bob Builder       18           16            -16
3       Bob Builder       18           0              40

【问题讨论】:

  • 从内部查询中删除and useruid in (1,2,3),看看它会给你什么。
  • 更新前的数据库是什么样的?
  • 问题是没有唯一的键来区分记录。您需要创建一个唯一的密钥。

标签: sql sql-server case-statement update-statement


【解决方案1】:

或者这个(与您的预期结果相同)

UPDATE a
    SET NonWorkHrs = ABS(sub.minNegativeTime)
FROM DME a
INNER JOIN (
    SELECT useruid, MIN(TimeEntered) minNegativeTime
    FROM DME b
    WHERE b.TimeEntered < 0
    GROUP BY Useruid
) sub ON sub.Useruid = a.Useruid

【讨论】:

    【解决方案2】:

    这个查询应该可以,试试看。

    DECLARE @DME TABLE
    (
    Useruid int,
    UserFullName varchar(200),
    NonWorkHrs  int,
    VacationHours   int,
    TimeEntered int
    )
    
    INSERT INTO @DME
    VALUES
    (1,'Jane Doe',18,8,-8),
    (1,'Jane Doe',18,0,10),
    (1,'Jane Doe',18,0,12),
    (2,'John Doe',18,18,-18),
    (2,'John Doe',18,0,23),
    (3,'Bob Builder',18,16,-16),
    (3,'Bob Builder',18,0,40)
    
    SELECT d.* FROM @DME d
    
        UPDATE @DME
            SET NonWorkHrs = T2.VacationHours
        FROM @DME T1
        INNER JOIN (SELECT Max(VacationHours) AS VacationHours, Useruid FROM @DME GROUP BY Useruid) AS T2 
        ON T2.Useruid = T1.Useruid
    
    
    SELECT d.* FROM @DME d
    

    【讨论】:

      【解决方案3】:

      使用CTE

        ;WITH vacationsPerUser as (
            SELECT userId, max(VacationHours) as maxVacationHours
            FROM DME 
            WHERE useruid in (1,2,3)
              and NonWorkHrsHolder > 0
            GROUP BY userId
           )
        Update b
          SET b.NonWorkHrs = ISNULL(vacationsPerUser.maxVacationHours, 0)
          FROM DME b 
          LEFT JOIN vacationsPerUser
               ON vacationsPerUser.UserId = b.UserId
          WHERE b.useruid in (1,2,3);
      

      【讨论】:

      • CTE 运行良好 Renat。太感谢了。我想我需要花更多时间了解 CTE :-)
      • 只是想补充一下,我可以使用此 CTE 以类似的方式更新其他列。再次感谢
      猜你喜欢
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 2013-12-13
      • 2021-10-14
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多