【问题标题】:Calculate contribution计算贡献
【发布时间】:2017-03-03 08:17:47
【问题描述】:

max(count()) 是不合法的,那么我该如何完成这里所示的贡献计算(以及其他列)

SELECT id,
       Avg(time)      AS avgSec,
       Stdev(time)    AS stdevSec,
       Count(time)    AS cnt,
       Avg(time)*Count(time)/max(Count(time))  AS contribution
FROM ...very long and complex query...

【问题讨论】:

  • 贡献的公式是什么
  • 我不想重复很长很复杂的查询
  • @Prdp avg * cnt / max(cnt) 如sql中所述
  • 你有 count(time) 函数,但你没有聚合任何东西,因为你会得到一个计数值。为单个值取最大值是没有意义的。有一个 group by 子句,它可以为您提供预期的结果

标签: sql sql-server


【解决方案1】:

使用MAX()OVER()窗口聚合函数获取所有记录的最大计数

这是正确的方法

Avg(time)*Count(time)/max(Count(time)) over()

【讨论】:

  • Over() 正在按照您首先编写的方式工作。 Over(partition) 给出错误的结果
  • @Cine - 好吧,也许我看过了
猜你喜欢
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 2016-11-21
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多