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

How to get the average of every three records in a column starting from first record in MS Access/SQL?

我正在做一些事情,我无法从列中的第一条记录开始获取每 three/four/five 条记录的平均值。如果我有一个 table 数据说

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 查询中获得这种输出。

如有任何帮助,我们将不胜感激。

谢谢, 亲爱的

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 

输出

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)

解释:

  • 外部查询: 对于mytableC中的每条记录,直到MyParam(问题中的 3、4 或 5),条记录在最后一条记录之前。
    where 子句中的查询中表示:FROM myTable As C WHERE C.ID_Col1 + [MyParam] -1 <= (SELECT MAX (D.ID_Col1) From myTable As D)
  • 内部查询: 计算MyParam条记录的平均值Value_Col2,从当前记录开始。
    Select 语句中表示:SELECT Avg(A.Value_Col2) 并在 Where 子句中表示:WHERE A.ID_Col1 >= C.ID_Col1,因为 C.ID_Col1current 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

考虑对最后三个 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 是自动编号字段,则无法保证值将保留在数字序数计数中。因此,派生的 table 和聚合子查询都需要计算出的行号 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