如何从MS Access / SQL中的第一个记录开始获取列中每三个记录的平均值?

时间:2016-07-13 16:59:40

标签: sql sql-server ms-access ms-access-2003

我正在研究一些事情,我从一列中的第一个记录开始,每隔三/四/五个记录就得到平均值。如果我有一个数据表

ID_Col1 | Value_Col2
1       | 1.5
2       | 2
3       | 2.5
4       | 3
5       | 3.5
6       | 4
7       | 4.5
8       | 5
9       | 5.5
10      | 6

如果我们说每三个记录的平均值,那么所需的输出是

    every_three_records_average_Column
    none
    none
    average(1.5, 2, 2.5)
    average(2, 2.5, 3)
    average(2.5, 3, 3.5)
    average(3, 3.5, 4)
    average(3.5, 4, 4.5)
    average(4, 4.5, 5)
    average(4.5, 5, 5.5)
    average(5, 5.5, 6)

有没有人有任何想法在SQL查询中获得这种输出。

非常感谢任何帮助。

谢谢, 蜂蜜

3 个答案:

答案 0 :(得分:1)

<强> SQL Fiddle Demo

SELECT 
     T1.[ID_Col1], T2.[ID_Col1], T3.[ID_Col1],
     T1.[Value_Col2] , T2.[Value_Col2] , T3.[Value_Col2],
     (T1.[Value_Col2] + T2.[Value_Col2] + T3.[Value_Col2])/3

FROM Source T1
JOIN Source T2
  ON T1.[ID_Col1] = T2.[ID_Col1] - 1 
JOIN Source T3
  ON T2.[ID_Col1] = T3.[ID_Col1] - 1 

<强>输出

enter image description here

答案 1 :(得分:1)

SELECT 
(
    SELECT Avg(A.Value_Col2) As Result
    FROM myTable As A
    WHERE A.ID_Col1 >= C.ID_Col1 and A.ID_Col1 < C.ID_Col1 + [MyParam]
)
FROM myTable As C
WHERE C.ID_Col1 + [MyParam] -1 <= (SELECT MAX (D.ID_Col1) From myTable As D)

说明:

  • 外部查询: 对于mytable C中的每条记录,直到MyParam (3,4或5)问题),记录最后一条记录。
    where子句中的查询中表示:FROM myTable As C WHERE C.ID_Col1 + [MyParam] -1 <= (SELECT MAX (D.ID_Col1) From myTable As D)
  • 内部查询: 计算Value_Col2条记录的MyParam平均值,启动当前记录。
    代表Select语句:SELECT Avg(A.Value_Col2)Where子句:WHERE A.ID_Col1 >= C.ID_Col1C.ID_Col1当前 ID,以及并且不超过[MyParam]条记录:A.ID_Col1 < C.ID_Col1 + [MyParam]

测试

MyTable:
ID_Col1 Value_Col2
1       1.5
2       2
3       2.5
4       3
5       3.5
6       4
7       4.5
8       5
9       5.5
10      6
11      6.5
12      7
13      7.5
14      8
15      8.5
16      9
17      9.5

MyParam = 3

的结果
Result
2
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5
9  

MyParam = 5

的结果
Result
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5

答案 2 :(得分:1)

考虑对最后三个ID进行过滤的相关聚合子查询:

SELECT myTable.ID_Col1, myTable.Value_Col2,

       (SELECT Avg(sub.Value_Col2)
        FROM myTable As sub
        WHERE sub.ID_Col1 >=  myTable.ID_Col1 - 2
        AND sub.ID_Col1 <=  myTable.ID_Col1
        AND myTable.ID_Col1 >= 3) As LastThreeAvg

FROM myTable;

输出

ID_Col1   Value_Col2  LastThreeAvg
1         1.5       
2         2     
3         2.5         2
4         3           2.5
5         3.5         3
6         4           3.5
7         4.5         4
8         5           4.5
9         5.5         5
10        6           5.5

但是,如果ID_Col1是自动编号字段,则没有保证值将保留在数字序数中。因此,派生表和聚合子查询中都需要计算的行号RowNo。在没有CTE的MS Access SQL中,查询变得有点冗长:

SELECT dT.ID_Col1, dT.Value_Col2, 

       (SELECT Avg(sub.Value_Col2)
        FROM 
             (SELECT ID_Col1, Value_Col2,
                     (SELECT Count(*) 
                      FROM myTable As sub
                      WHERE sub.ID_Col1 <= myTable.ID_Col1) As RowNo
             FROM myTable) As sub
        WHERE sub.RowNo >=  dT.RowNo - 2
        AND sub.RowNo <=  dT.RowNo
        AND sub.RowNo >= 3) As LastThreeAvg
FROM

(SELECT ID_Col1, Value_Col2,
        (SELECT Count(*) 
         FROM myTable As sub
         WHERE sub.ID_Col1 <= myTable.ID_Col1) As RowNo
FROM myTable) As dT