如何根据字段的总和更新记录,然后使用总和在sql中计算新值

发布于 2024-09-03 06:45:50 字数 974 浏览 3 评论 0原文

以下是我试图通过迭代记录来完成的操作。

如果可能的话,我希望有一个更优雅的解决方案,因为我确信这不是在 sql 中执行此操作的最佳方法。

set @counter = 1 

declare @totalhrs dec(9,3), @lastemp char(7), @othrs dec(9,3) 

while @counter <= @maxrecs 
begin 
  if exists(select emp_num from #tt_trans where id = @counter) 
  begin 
    set @nhrs = 0 
    set @othrs = 0 

    select @empnum = emp_num, @nhrs = n_hrs, @othrs = ot_hrs 
    from #tt_trans 
    where id = @counter 

    if @empnum = @lastemp 
    begin 
      set @totalhrs = @totalhrs + @nhrs 

      if @totalhrs > 40 
      begin 
        set @othrs = @othrs + @totalhrs - 40 
        set @nhrs = @nhrs - (@totalhrs - 40) 
        set @totalhrs = 40 
      end 
    end 
    else 
    begin 
       set @totalhrs = @nhrs 
       set @lastemp = @empnum 
    end 

    update #tt_trans 
    set n_hrs = @nhrs, 
        ot_hrs = @othrs 
    where id = @counter and can_have_ot = 1 
  end 

  set @counter = @counter + 1 
end

谢谢

Below is what I'm trying to do with by iterating through the records.

I would like to have a more elegant solution if possible since I'm sure this is not the best way to do it in sql.

set @counter = 1 

declare @totalhrs dec(9,3), @lastemp char(7), @othrs dec(9,3) 

while @counter <= @maxrecs 
begin 
  if exists(select emp_num from #tt_trans where id = @counter) 
  begin 
    set @nhrs = 0 
    set @othrs = 0 

    select @empnum = emp_num, @nhrs = n_hrs, @othrs = ot_hrs 
    from #tt_trans 
    where id = @counter 

    if @empnum = @lastemp 
    begin 
      set @totalhrs = @totalhrs + @nhrs 

      if @totalhrs > 40 
      begin 
        set @othrs = @othrs + @totalhrs - 40 
        set @nhrs = @nhrs - (@totalhrs - 40) 
        set @totalhrs = 40 
      end 
    end 
    else 
    begin 
       set @totalhrs = @nhrs 
       set @lastemp = @empnum 
    end 

    update #tt_trans 
    set n_hrs = @nhrs, 
        ot_hrs = @othrs 
    where id = @counter and can_have_ot = 1 
  end 

  set @counter = @counter + 1 
end

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-09-10 06:45:50

这很接近您想要的,但是一旦您回答了我关于您真正想要实现的目标的评论,就需要进行一些调整。

update #tt_trans 
    set n_hrs = CASE WHEN T2.totHrs>40 
                     THEN 40 
                     ELSE T2.totHrs END,
    ot_hrs= CASE WHEN T2.totHrs>40 
                 THEN T2.totHrs-40 
                 ELSE 0 END
FROM  #tt_trans trans T1
INNER JOIN (SELECT SUM(@nhrs) totHrs, EmpNum 
           FROM #tt_trans 
           WHERE can_have_ot=1 
           GROUP BY EmpNum) T2 ON (T1.EmpNum=T2.EmpNum)

WHERE can_have_ot = 1 

This is close to what you want, but will need to be tuned a bit once you answer my comment about what you are really trying to achieve.

update #tt_trans 
    set n_hrs = CASE WHEN T2.totHrs>40 
                     THEN 40 
                     ELSE T2.totHrs END,
    ot_hrs= CASE WHEN T2.totHrs>40 
                 THEN T2.totHrs-40 
                 ELSE 0 END
FROM  #tt_trans trans T1
INNER JOIN (SELECT SUM(@nhrs) totHrs, EmpNum 
           FROM #tt_trans 
           WHERE can_have_ot=1 
           GROUP BY EmpNum) T2 ON (T1.EmpNum=T2.EmpNum)

WHERE can_have_ot = 1 
傲影 2024-09-10 06:45:50

看起来您正在迭代,因为您必须跨多个记录跟踪给定员工的总工时。您可以改为使用内部选择来汇总每个员工的小时数,并将该选择加入到 #tt_trans 表中。从该连接写入更新,并将逻辑放入更新列的 CASE 语句中。

It looks like you are iterating because you have to keep track of the total hours for a given employee across multiple records. You can instead use an interior select to sum the hours for each employee, and join that select to your #tt_trans table. Write your update from that join, and put your logic in CASE statements for the updating columns.

樱娆 2024-09-10 06:45:50
update #tt_trans
    set n_hrs = case 
    when t2.totHrs > 40
    then n_hrs - (t2.totHrs -40)
    else n_hrs
    end,
    ot_hrs = case 
    when t2.totHrs > 40 
    then ot_hrs + (t2.totHrs -40)
    else ot_hrs
    end
    from #tt_trans t1
    join (select sum (n_hrs) as totHrs, emp_num from #tt_trans where can_have_ot =1 group by emp_num) t2 on t1.emp_num = t2.emp_num where t1.post_date = (select max(post_date)from #tt_trans where emp_num = t1.emp_num)

基本上我修改了该期间最后一个日期的时间并进行了相应调整。感谢两位的回复。这两个回复都让我想到了我的。

update #tt_trans
    set n_hrs = case 
    when t2.totHrs > 40
    then n_hrs - (t2.totHrs -40)
    else n_hrs
    end,
    ot_hrs = case 
    when t2.totHrs > 40 
    then ot_hrs + (t2.totHrs -40)
    else ot_hrs
    end
    from #tt_trans t1
    join (select sum (n_hrs) as totHrs, emp_num from #tt_trans where can_have_ot =1 group by emp_num) t2 on t1.emp_num = t2.emp_num where t1.post_date = (select max(post_date)from #tt_trans where emp_num = t1.emp_num)

Basically I took the modified the hours of the last date in the period and adjusted accoringly. Thanks to the both of you who responded. Both replies led me to mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文