【问题标题】:Update table records with accumulated result使用累积结果更新表记录
【发布时间】:2014-11-25 13:38:56
【问题描述】:

假设我有一个表 Tbl(代表针对不同客户所做的工作的简单时间日志)

五列 编号:整数 时间使用:浮动 IdCustomer:整数 创建:日期时间 时间计算:浮动

我在这个表中有很多条记录,(TimeCalc被初始化为value = 0)

我希望我的 SQL 做的是:

当特定客户的所有上述记录的 TimeUse 累积到值

当特定客户的所有上述记录的 TimeUse 累积到一个值 >= 10 时,TimeCalc 中的值应该是 = TimeUse for the record...

我用带有子查询的 Case 例程搞砸了,但无法让它工作。

BEFORE
    Id        TimeUse       IdCustomer        Created        TimeCalc
    1         2             1                 14/09/09       0
    2         5             2                 14/09/10       0
    3         2             1                 14/09/11       0
    4         5             2                 14/09/12       0
    5         4             1                 14/09/13       0
    6         2             2                 14/09/14       0
    7         4             1                 14/09/15       0
    8         1             1                 14/09/16       0
    9         3             2                 14/09/17       0
    10        2             1                 14/09/18       0
    11        4             2                 14/09/19       0



AFTER
    Id        TimeUse       IdCustomer        Created        TimeCalc
    1         2             1                 14/09/09       0
    2         5             2                 14/09/10       0
    3         2             1                 14/09/11       0
    4         5             2                 14/09/12       0
    5         4             1                 14/09/13       0
    6         2             2                 14/09/14       2
    7         4             1                 14/09/15       0
    8         1             1                 14/09/16       1
    9         3             2                 14/09/17       3 
    10        2             1                 14/09/18       2
    11        4             2                 14/09/19       4

这可以在 SQL 更新中解决吗?

【问题讨论】:

    标签: sql sql-server tsql stored-procedures


    【解决方案1】:

    在 SQL Server 2012+ 中,您可以使用累积和来执行此操作:

    select Id, TimeUse, IdCustomer, Created,
           (case when sum(timeuse) over (partition by idcustomer order by id) < 10 then 0
                 else timeuse
            end) as timecalc
    from table t;
    

    您可以在早期版本中使用outer apply 或子查询来执行相同的操作。

    如果您想要更新,只需使用 CTE:

    with toupdate as (
          select t.*,
                 (case when sum(timeuse) over (partition by idcustomer order by id) < 10 then 0
                       else timeuse
                  end) as new_timecalc
          from table t
         )
    update toupdate
        set timecalc = new_timecalc;
    

    编辑:

    以下内容适用于任何版本的 SQL Server:

    with toupdate as (
          select t.*,
                 (case when (select sum(t2.timeuse)
                             from table t2
                             where t2.idcustomer = t.idcustomer and
                                   t2.id <= t.id
                            ) < 10 then 0
                       else timeuse
                  end) as new_timecalc
          from table t
         )
    update toupdate
        set timecalc = new_timecalc;
    

    【讨论】:

    • 嗨,谢谢.. 这些是否适用于 SQL Server 2012?
    • @Beaker 。 . .全部。
    • 您确定您使用的是具有正确兼容模式的 SQL Server 2012?
    • 既然我不知道你所说的兼容模式是什么意思,我必须说不:)
    • 最新编辑有效。不知道为什么其他人不-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2020-02-13
    相关资源
    最近更新 更多