使用聚合函数和算术运算

时间:2016-12-13 14:53:13

标签: sql function aggregate operation

我有一个包含两列的表

enter image description here

这里我需要根据以下函数找到一个SQL,

Z = accelx-AVG(accelx) for every rows. 

5 个答案:

答案 0 :(得分:2)

这通常使用AVG的分析版本来完成:

select accelx - avg(accelx) over ()
from mytable;

答案 1 :(得分:0)

只需在子查询中计算AVG

SELECT AccelX - (SELECT AVG(AccelX) FROM T)
FROM T

答案 2 :(得分:0)

试试这个:

select t1.accelx - t2.avgx as Z
from TableName as t1
inner join (
    select avg(accelx) as avgx
    from TableName
    ) as t2
on 1 = 1

答案 3 :(得分:0)

SELECT t1.accelx - t2.avgx as Z, userName 
FROM TableName as t1
JOIN (SELECT userName, avg(accelx) as avgx
      FROM TableName
      GROUP BY userName) as t2       
  ON t1.userName = t2.userName

答案 4 :(得分:0)

MSSQL Server上的TSQL具有Windowed功能。 OVER Clause

SELECT 
    [AccelX]
    ,[AccelX] - AVG([AccelX]) OVER ()
FROM T