【问题标题】:Calculate running total with over(partition by order by) within update statment在更新语句中使用 over(partition by order by) 计算运行总计
【发布时间】:2017-11-02 17:05:53
【问题描述】:

我正在尝试创建按升序排列的产品子组的总收入。

我使用了以下查询

Update Z_Discount_Tool1 Set Cum_Net_Revenue_Discount = 
(Select Sum([Umsatz_Netto]) Over (Partition by [WUGR_act] Order by [Discount] From Z_Discount_Tool1)

但是,我收到以下错误消息

子查询返回超过 1 个值。当子查询跟随 =、!=、、>= 或子查询用作表达式时,这是不允许的。

我在我的代码中找不到错误。所以任何帮助表示赞赏。提前致谢!

【问题讨论】:

  • 根据语法,我添加了sql-server标签。

标签: sql sql-server cumulative-sum


【解决方案1】:

这看起来像 SQL Server。您可以使用可更新的 CTE:

with toupdate as (
      select dt.*,
             Sum([Umsatz_Netto]) Over (Partition by [WUGR_act] Order by [Discount]) as new_Cum_Net_Revenue_Discount
      from Z_Discount_Tool1 dt
     )
Update toupdate
     Set Cum_Net_Revenue_Discount = new_Cum_Net_Revenue_Discount;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多