根据前一行对Access中的行求和

时间:2018-01-09 18:49:51

标签: sql database ms-access

基本上我有一个Access查询集来返回给定日期的所有条目。在那一天,有#34;赶上所有"条目(被称为'每小时'),只是说一个小时的观察窗口开始时。然后,如果在那个小时内观察到任何事物,则输入一个唯一的ID,如下所示。

| Date       | Time  | ID      | Amount |
|------------|-------|---------|--------|
| 11/14/2017 | 10:30 | Hourly  | 0      |
| 11/14/2017 | 10:34 | Unique1 | 1      |
| 11/14/2017 | 10:46 | Unique2 | 1      |
| 11/14/2017 | 11:17 | Unique1 | 1      |
| 11/14/2017 | 11:30 | Hourly  | 0      |
| 11/14/2017 | 11:49 | Unique1 | 1      |
| 11/14/2017 | 12:30 | Hourly  | 0      |
| 11/14/2017 | 12:43 | Unique1 | 1      |
| 11/14/2017 | 12:51 | Unique1 | 1      |
| 11/14/2017 | 13:01 | Unique2 | 1      |

我们希望根据ID'每小时'推出小时摘要,其中所有其他条目被合并/汇总到每小时'每小时' ID,如下所示:

Date        Time     ID   Amount
11/14/2017  10:30   Hourly  3
11/14/2017  11:30   Hourly  1
11/14/2017  12:30   Hourly  3

我们已经尝试根据当时的小时字段对条目进行求和,但最终看起来像:

 Date        Hour     Amount
11/14/2017    10        2
11/14/2017    11        2
11/14/2017    12        2
11/14/2017    13        1

不幸的是,在运行此输出的统计数据时,每小时平均值为1.75 /小时,而实际上它是2.33 /小时。

有没有办法指定如果一个条目是'每小时',那么将所有条目加起来直到下一个小时'条目?还是会让我们到达类似地方的任何其他想法?

谢谢!

2 个答案:

答案 0 :(得分:1)

我假设日期和时间存储在单个日期/时间字段中。如果不是这种情况,则必须相应调整。

我在MS Access 2007中创建了以下两个“查询”:

/* testdata_daterange */
SELECT a.Date as D1, Min(b.Date) AS D2
FROM testdata AS a
LEFT JOIN testdata AS b ON (a.Date < b.Date and b.id='Hourly')
WHERE a.id='Hourly'
GROUP BY a.Date;

/* testdata_sum */
SELECT testdata_daterange.D1, testdata_daterange.D2, SUM(Amount) AS Total
FROM testdata
INNER JOIN testdata_daterange ON (testdata_daterange.D1 <= testdata.Date) AND (testdata.Date < testdata_daterange.D2 or testdata_daterange.D2 is null)
WHERE testdata.ID<>'Hourly'
GROUP BY testdata_daterange.D1, testdata_daterange.D2;

结果:

Output

答案 1 :(得分:-1)

以下不是最好的代码,但可能有助于您入门:

Function SummariseHourly()
    Dim dbsCdb As DAO.Database
    Dim rstInp As DAO.Recordset
    Dim rstOut As DAO.Recordset
    Dim lngAmt As Long
    Dim flgEnd As Boolean

    Set dbsCdb = CurrentDb
    Set rstInp = dbsCdb.OpenRecordset("SELECT * FROM InputTable ORDER BY Date, Time")
    Set rstOut = dbsCdb.OpenRecordset("OutputTable")

    If Not rstInp.EOF Then ' If there are records in the Input Table
        rstInp.MoveFirst ' Move to the first one
        Do Until rstInp.EOF ' Whilst there are still records in the Input Table
            If rstInp!id = "Hourly" Then ' If the "ID" of the current record is "Hourly"
                rstOut.AddNew ' Add a new record to the Output Table
                rstOut!Date = rstInp!Date ' Populate the "Date"
                rstOut!Time = rstInp!Time ' Populate the "Time"
                rstOut!id = "Hourly" ' Populate the "ID"
                rstInp.MoveNext ' Move onto the next record
                lngAmt = 0 ' Initialise a variable to store the total of "Amount"
                flgEnd = False ' Initialise a variable to cease the inner loop
                Do Until rstInp.EOF Or flgEnd ' Whilst there are still records
                    If rstInp!id = "Hourly" Then ' If the "ID" is "Hourly"
                        flgEnd = True ' Exit the loop
                    Else
                        lngAmt = lngAmt + rstInp!Amount ' Else add the "Amount" stored with the record to our total
                        rstInp.MoveNext ' Move onto the next record
                    End If
                Loop
                rstOut!Amount = lngAmt ' Populate the "Amount" in the new record with our total
                rstOut.Update ' Update the Output Table with the new record
            Else
                rstInp.MoveNext ' Else we haven't reached an "Hourly" record yet
            End If
        Loop
    End If

    rstInp.Close
    rstOut.Close
End Function

代码假定您的数据驻留在名为"InputTable"的表中,并且存在一个名为"OutputTable"的结构相同的表。