Comment puis-je sélectionner un seul enregistrement par « personne », par jour avec une jointure interne dans une requête MS Access?

StackOverflow https://stackoverflow.com/questions/884393

Question

Je dois extraire des données de deux tables: Neptune_FN_Analysis et Neptune_prem Il y aura 3 champs appelés readings_miu_id (comparable à un nom de personnes ou de l'article #), ReadDate, ReadTime (qui sont tous en Neptune_FN_Analysis). Certains readings_miu_ids ont plusieurs ReadTimes pour plusieurs jours, mais je veux seulement tirer la « dernière fois » entré par readings_miu_id, par jour.
Je dois tous les readings_miu_ids qui ont une date d'entrée pour la plage sélectionnée, mais seule la dernière ReadTime entrée pour chaque enregistrement je tire.

Ma solution à ce jour, sur la base d'une table est:

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

Lorsque je tente d'adapter cette solution, je ne peux pas faire le FROM [tableName] as A, INNER JOIN car il me donne une erreur. Le code original que mon prédécesseur a fait (ce qui est ce que je suis en train d'adapter / fix) est la suivante:

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'
Était-ce utile?

La solution

Je ne comprends pas tout à fait tout ce que vous essayez de faire, mais si vous INNER JOIN sur une sous-requête qui obtient le MAX de ce jour, il pourrait éliminer tous les enregistrements dont la date n'a pas été au maximum

SELECT readings_miu_id, Reading, ReadDate, ReadTime, MIUwindow, SN, 
Noise, RSSI, OriginCol, ColID, Ownage 
FROM analyzed 
INNER JOIN 
(SELECT [whatever the id common to all like records is] as likeID, MAX(analyzed.ReadTime) as latestDate
 FROM analyzed 
 GROUP BY likeID) AS maxDate ON analyzed.likeID=maxDate.likeID AND analyzed.latestDate = maxDate.latestDate
WHERE ReadDate Between #4/21/2009# and #4/29/2009# 

modifier selon les besoins

Autres conseils

Je voudrais essayer quelque chose comme ceci:

SELECT a.readings_miu_id, a.Reading, a.ReadDate, a.ReadTime, a.MIUwindow, a.SN, a.Noise, a.RSSI, a.OriginCol, a.ColID, a.Ownage
FROM analyzed AS A INNER JOIN
          (SELECT max(ReadTime) as MaxReadTime,readings_miu_id FROM analyzed 
             WHERE ReadDate Between #4/21/2009# and #4/29/2009#
              GROUP BY readings_miu_id) as B 
             on a.readings_miu_id = b.readings_miu_id  and a.MaxReadTime = b.ReadTime
SELECT
     <your columns>
FROM
     Neptune_FN_Analysis A1
INNER JOIN Neptune_prem ON
     P.premice_id = A1.PremID
LEFT OUTER JOIN Neptune_FN_Analysis A2 ON
     A2.readings_miu_id = A1.readings_miu_id AND
     A2.ReadDate = A1.ReadDate AND
     A2.ReadTime > A1.ReadTime
WHERE
     A2.readings_miu_id IS NULL AND
     <add any additional criteria here>

Je ne sais pas ce que vous laissez entendre en spécifiant « INNER JOIN » cette fois. D'autres réponses utilisent une sous-requête, alors voici un exemple en utilisant deux INNER JOINs et pas sous-requête. Plutôt que d'obtenir ma tête autour de votre schéma :) J'utilise Northwind pour revenir clients et la date de leur ordre le plus récent:

SELECT C1.CustomerID, C1.CompanyName, 
       O1.OrderID, O1.OrderDate
  FROM (Customers AS C1
       INNER JOIN Orders AS O1
          ON C1.CustomerID = O1.CustomerID)
       INNER JOIN Orders AS O2
          ON C1.CustomerID = O2.CustomerID
 GROUP 
    BY C1.CustomerID, C1.CompanyName, 
       O1.OrderID, O1.OrderDate
HAVING O1.OrderDate = MAX(O2.OrderDate);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top