具有条件的访问中的查找值

时间:2019-02-07 10:31:47

标签: ms-access

我有一个访问数据库,其中包含一系列有关自行车道的值:日期,参与者,参与计数,赛道距离...几年后,这里有大量数据。现在,我正在尝试寻找一种方法,让参与者完成1000公里的路程后进行查找。 (日期和轨道号)。像这样:

计算参与者A的KM,然后看他何时达到1000公里。 他参加了25 966KM比赛,参加了26 1009KM比赛。

然后在报告中显示:参与者A在日期x和第26种族越过了1000公里。

1 个答案:

答案 0 :(得分:0)

给出一个表格Races,其中包含字段ParticipantIDRaceIDDistanceRaceDate,这应该是您的:

Select
    Races1000.ParticipantID,
    First(Races1000.RaceID) As RaceID,
    First(Races1000.RaceDate) As RaceDate,
    First(Races1000.DistanceSum) As DistanceSum
From
    (
        Select
            Races.*,
            (
                Select
                    Sum(Distance)
                From
                    [Races] As S
                Where
                    S.RaceDate <= Races.RaceDate And
                    S.ParticipantID = Races.ParticipantID
            ) As DistanceSum
        From
            Races
        Where
            (
                Select
                    Sum(Distance)
                From
                    [Races] As S
                Where
                    S.RaceDate <= Races.RaceDate And
                    S.ParticipantID = Races.ParticipantID
            ) >= 1000
    ) As Races1000
Group By
    Races1000.ParticipantID
Order By
    Races1000.ParticipantID,
    First(Races1000.RaceDate),
    First(Races1000.DistanceSum)

如果有这样的表,请使用提供的SQL语句创建查询,以查看该表是否对您有用。 或者只是将表名和字段名修改为您的姓名。

编辑:

要基于保存的查询创建报告,请在数据库窗口中选择查询,然后在RibbonBar的选项卡Report中按Create。它将基于所选查询创建标准报告:

enter image description here

关于“ 2000、3000,... KM限制”问题,您可以将>= 1000更改为>= [KM],以便能够使用相同的查询并报告任何距离限制。打开查询时,系统将询问您距离,您可以输入距离。