SubQueries in MS ACCESS: select only one record per person per date

I use a table that is parsed in Microsoft Access. It has many fields, but the three that are used for filtering in this case are parsed .readings_miu_id, parsed .ReadDate, parsed. ReadTime I need to pull records from the “parsed” table, where readings_miu_id are grouped together and then sorted by ReadDate, but only the last record for the specified date is displayed, which may be the highest time value in ReadTime. I created a query:

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);

* ???? *** used to show that im not sure what to put here

if I enter a valid readings_miu_id, I get one record with the highest time for this readings_miu_id.

, , ReadTime _miu_id ReadDate?

readings_miu_id ,

+3
2
 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 . , ReadDate .

+4

. " 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
0

Source: https://habr.com/ru/post/1708663/


All Articles