2016-12-13 104 views

回答

0

子查詢簡單地計算AVG

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

使用窗口功能。 'AVG(AccelX)OVER()' –

+0

@MthetheWWhited不是每個db都有它 –

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 
+1

'on 1 = 1'?這是一個交叉連接,所以你應該用'交叉連接'替換'內部連接',並刪除虛擬的'on'子句。 –

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 
2

這通常使用的AVG解析版本完成:MSSQL Server上

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

你是對的,但不是所有的數據庫都有。我會包括'按用戶名分區' –

+0

我也會,如果我想每個用戶的平均。我不這樣做,如果這是OP想要的,但是;-) –

0

TSQL已窗口功能。 OVER Clause

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