I need to pull data from two tables: Neptune_FN_Analysis
and Neptune_prem
There will be 3 fields called readings_miu_id
(comparable to a persons name or item #), ReadDate
, ReadTime
(all of which are in Neptune_FN_Analysis
). Some readings_miu_id
s have multiple ReadTime
s for multiple days but I only want to pull the "last time" entered per readings_miu_id
, per day.
I need all readings_miu_id
s that have an entry date for the selected range but only the last ReadTime
entered for each record I am pulling.
My solution so far, based on one table is:
SELECT readings_miu_id, Reading, ReadDate, ReadTime, MIUwindow, SN, Noise, RSSI, OriginCol, ColID, Ownage
FROM analyzed AS A
WHERE ReadDate Between #4/21/2009# and #4/29/2009#
AND 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);
When I try to adapt this solution, I can't do the FROM [tableName] as A, INNER JOIN
because it gives me an error. The original code that my predecessor made (which is what I am trying to adapt/fix) is as follows:
SELECT readings_miu_id, Reading, ReadDate,Format([MIUtime],'hh:mm:ss') AS
ReadTime, MIUwindow, SN, Noise, RSSI, ColRSSI, MIURSSI, Firmware, CFGDate, FreqCorr,
Active, MeterType, OriginCol, ColID, Ownage, SiteID, PremID, Neptune_prem.prem_group1,
Neptune_prem.prem_group2, ReadID
INTO analyzed
FROM Neptune_FN_Analysis INNER JOIN
Neptune_prem ON Neptune_FN_Analysis.PremID = Neptune_prem.premice_id
WHERE SiteID = 36801 and ReadDate BETWEEN #04/21/09# AND #04/27/09#
and OriginCol = 'US 29' and ColID = 1 and ColID <> 0 and Active = 'Y'