我正在使用Microsoft Access中分析的表。它有很多字段,但在这种情况下用于过滤的三个字段被分析.readings_miu_id,analyze.ReadDate,analyze.ReadTime。我需要从“已分析”表中提取记录,其中readings_miu_id组合在一起,然后按ReadDate排序,但仅显示所述日期的最后一个条目,这可能是ReadTime中的最高值。我创建了一个查询:
SELECT readings_miu_id, Reading, ReadDate, ReadTime,
MIUwindow, SN, Noise, RSSI, OriginCol, ColID, Ownage
FROM analyzed
WHERE analyzed.ReadDate Between #4/21/2009# and #4/29/2009#
AND analyzed.ReadTime= (
SELECT TOP 1 analyzed.ReadTime
FROM analyzed
WHERE analyzed.readings_miu_id = *????***
ORDER BY analyzed.ReadTime DESC);
* ???? ***用于表明我不知道该放什么
如果我输入有效的readings_miu_id,我会得到一条记录,其中readings_miu_id的时间最长。
如何使用上面的子查询仅为每个ReadDate读取每个readings_miu_id的最后一个ReadTime?
readings_miu_id 不是表格ID,更类似于项目编号或名称
答案 0 :(得分:4)
SELECT readings_miu_id
, Reading , ReadDate , ReadTime
, MIUwindow, SN, Noise, RSSI
, OriginCol, ColID, Ownage
FROM analyzed AS A
WHERE analyzed.ReadDate Between #4/21/2009# and #4/29/2009#
AND analyzed.ReadTime=
(SELECT TOP 1 analyzed.ReadTime FROM analyzed
where analyzed.readings_miu_id = A.readings_miu_id
AND analyzed.ReadDate = A.ReadDate
ORDER BY analyzed.ReadTime DESC);
我把主表别名为A,并在子查询中引用它。不确定是否需要按子查询中的“读取日期”进行过滤。
答案 1 :(得分:0)
这会让你接近。声明“readings_miu_id组合在一起”让我很困惑。不确定是否需要GroupBy或该字段的排序。此查询将为您提供分析表中具有相同日期但是该给定日期的“最大:”时间的记录。听起来像是你想要的。
SELECT readings_miu_id, Reading, ReadDate, ReadTime, MIUwindow, SN, Noise, RSSI, OriginCol, ColID, Ownage
FROM analyzed ,
(
Select ReadDate as matchDate, Max(ReadTime) as matchTime
FROM analyzed
Where analyzed.ReadDate Between #4/21/2009# and #4/29/2009#
Group by ReadDate
) dateAndTime
WHERE analyzed.ReadDate Between #4/21/2009# and #4/29/2009# AND ReadDate = dateAndTime.matchDate AND readTime = dateAndTime.matchTime
Order By readDate