使用查询SQL增加AVG

时间:2016-10-04 10:25:08

标签: sql

我有下表:

table

请帮我在SQL中使用以下查询:  “AVG(VALUE2 /值1,值3 /值2,值4 /值3)”

非常感谢!

4 个答案:

答案 0 :(得分:1)

对于SQL Server 2008或更高版本;

; WITH CTE AS
(
SELECT *, LAG(VALUE) OVER(ORDER BY MONTH) AS XX FROM YOURTABLE
) 
SELECT AVG(CAST(VALUE AS FLOAT) / CAST(XX AS FLOAT)) FROM CTE WHERE XX IS NOT NULL

答案 1 :(得分:1)

试试这个:

SELECT AVG(Calc)
FROM
(
  SELECT t2.Value/t1.Value AS Calc
  FROM [Table] t1
  JOIN [Table] t2
  ON t1.[Month] = t2.[Month] + 1
) calc

答案 2 :(得分:0)

尝试以下查询。

            select avg(CAST(a.value as FLOAT)/cast(a.mm as FLOAT)) from
            (
            SELECT *, LAG(value) OVER(ORDER BY MONTH) AS mm FROM #your_table
            ) 
            a where a.mm is not null

如果您有疑虑,请告诉我们

答案 3 :(得分:0)

我想使用查询SQL:

如果我选择month = 3 => result = AVG(value2 / value1,value3 / value2)-1

如果我选择month = 4 result = AVG(value2 / value1,value3 / value2,value4 / value3)-1

谢谢