如何从访问

时间:2017-08-16 11:21:28

标签: sql database ms-access ms-access-2013

以下是我的查询结果。但是有很多冗余记录,因此,我想过滤掉这个查询的结果。我的目标是每个角度只提取两个记录,第一个和最后一个。 例如,当角度 195 时, 我希望在日期= 2017年2月27日,时间=凌晨2点00:00 时获得第一张唱片, 当日期为 2/27/2017且时间= 9:00:00 AM 时的第二条记录。 类似地,当角度变为 210 时,我希望在日期= 2017年2月27日时间=上午10:00:00 和其他记录时获得第一条记录当日期和时间 2017年2月27日和晚上9:00:00 时 所有记录都是如此。 我试图自己做,但它只返回一个记录,每个角度只有前一个,不知道我怎么得到最后一个。 我正在使用两个查询(Query1)

SELECT final.Date, final.Angle
FROM final
GROUP BY final.Date, final.Angle

和第二个查询是(fileredOUtput)

SELECT Query1.Date, Query1.Angle, (SELECT TOP 1 final.Date FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle)
 AS NewDate,
 (SELECT TOP 1 final.Angle FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS NewAngle,
 (SELECT TOP 1 final.earthCol.Value FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS NewE_CV, 
(SELECT TOP 1 final.earthCol.ColNu FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS New_E_CN,
 (SELECT TOP 1 final.mars_Col.Value FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS NewM_CV,
 (SELECT TOP 1 final.Col_apart FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS New_CApart,
 (SELECT TOP 1 final.mars_Col.ColNu FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS NewM_CN, 
(SELECT TOP 1 final.Time FROM final WHERE  Query1.Date=final.Date AND Query1.Angle= final.Angle) AS NewTime
FROM Query1, final
WHERE (((Query1.Date) Between [Forms]![Query Form]![txtStartDate] And [Forms]![Query Form]![txtEndDate]));

Query results and results I need are marked with red。 期待着听到您的意见。 谢谢。

3 个答案:

答案 0 :(得分:0)

可能是:

SELECT 
    * 
FROM 
    final,

    (SELECT 
        final.Date, Min(final.Time) As MinTime, Max(final.Time) As MaxTime, final.Angle
    FROM 
        final
    GROUP BY 
        final.Date, final.Angle) As T

WHERE
    final.Angle = T.Angle AND
    final.Date = T.Date AND
    ((final.Time = T.MinTime) OR (final.Time = T.MaxTime))
ORDER BY
    final.Date, final.Time, final.Angle

答案 1 :(得分:0)

请记住,除非您还从表中选择时间,否则您无法知道要检索的值。所需输出中的日期和角度不是唯一的组合。

您可能需要稍微修改Access语法。

SELECT 
    final.Date AS dt, 
    MIN(final.Time) AS tm, 
    final.Angle AS ag
FROM 
    final 
GROUP BY 
    final.Date, final.Angle
UNION SELECT 
    final.Date, 
    MAX(final.Time), 
    final.Angle
FROM 
    final 
GROUP BY 
    final.Date, final.Angle
ORDER BY 
    dt, ag, tm;

编辑:SQL小提琴示例...... http://sqlfiddle.com/#!9/2f638c/14

答案 2 :(得分:0)

试试这个

SELECT * FROM final INNER JOIN
(SELECT Min(DDate + DTime) AS DDateTime, Angle FROM final GROUP BY Angle
UNION SELECT Max(DDate + DTime) AS DDateTime, Angle FROM final GROUP BY Angle) mm
ON final.DDate + final.DTime = mm.DDateTime AND final.Angle = mm.Angle

请注意,在测试中我分别将前两列的名称更改为DDate和DTime,因为日期和时间是保留字。

修改

这使得它变得相当困难,特别是在没有LEAD / LAG功能的Access中。以下应该可行,但有人可能会有一个更优雅的解决方案!

SELECT final.* FROM final INNER JOIN
(SELECT MIN(DDateTime) AS MDateTime, Angle FROM
(SELECT (f.DDate+ f.DTime) AS DDateTime, f.Earth_Value, f.Mars_Value, f.Earth_Col, f.Mars_Col, f.Diff, f.Angle, f.Col_Apart,
IIF(ISNULL((SELECT MIN(m.DDate + m.DTime) FROM final m where f.angle <> m.angle and (f.DDate+f.DTime) < (m.DDate+m.DTime))),
(SELECT MAX(DDate+DTime) FROM final),(SELECT MIN(m.DDate + m.DTime) FROM final m where f.angle <> m.angle and (f.DDate+f.DTime) < (m.DDate+m.DTime))) AS NextChangeDateTime
FROM final f order by DDate, DTime) g
GROUP BY g.Angle,g.NextChangeDateTime
UNION
SELECT MAX(DDateTime) AS MDateTime, Angle FROM
(SELECT (f.DDate+ f.DTime) AS DDateTime, f.Earth_Value, f.Mars_Value, f.Earth_Col, f.Mars_Col, f.Diff, f.Angle, f.Col_Apart,
IIF(ISNULL((SELECT MIN(m.DDate + m.DTime) FROM final m where f.angle <> m.angle and (f.DDate+f.DTime) < (m.DDate+m.DTime))),
(SELECT MAX(DDate+DTime) FROM final),(SELECT MIN(m.DDate + m.DTime) FROM final m where f.angle <> m.angle and (f.DDate+f.DTime) < (m.DDate+m.DTime))) AS NextChangeDateTime
FROM final f order by DDate, DTime) g
GROUP BY g.Angle,g.NextChangeDateTime) FLDates
ON final.DDate + final.DTime = FLDates.MDateTime

编辑2

从技术上讲,MS Access中没有临时表。实际上,您只需创建一个普通表,并在每次要使用它时删除其内容。

要创建表,您需要将其复制到查询窗口(SQL视图),然后单击运行:

CREATE TABLE final (
    DDate       DATETIME     NOT NULL,
    DTime       DATETIME     NOT NULL,
    Earth_Value         DOUBLE   NOT NULL,
    Mars_Value         DOUBLE   NOT NULL,
    Earth_Col           INTEGER     NOT NULL,
    Mars_Col           INTEGER     NOT NULL,
    Diff           INTEGER     NOT NULL);

然后在您现有的查询类型之前(您只需要第一行):

INSERT into final
SELECT etc.

现在,您将能够完全按原样运行我的查询。