0

我需要从两个表中提取数据:Neptune_FN_Analysis并且Neptune_prem 将有 3 个字段称为readings_miu_id(与人名或项目 # 相当)ReadDate,,ReadTime(所有这些都在 中Neptune_FN_Analysis)。有些readings_miu_ids 有多个ReadTimes 多天,但我只想提取readings_miu_id每天输入的“最后一次”。
我需要所有readings_miu_id具有所选范围的输入日期的 s,但只有ReadTime我要提取的每条记录的最后输入日期。

到目前为止,我基于一张表的解决方案是:

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

当我尝试调整此解决方案时,我不能这样做,FROM [tableName] as A, INNER JOIN因为它给了我一个错误。我的前任制作的原始代码(这是我正在尝试修改/修复的)如下:

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'
4

4 回答 4

2

我不太明白你想要做的所有事情,但是如果你在一个获得日期最大值的子查询上进行内部连接,它可以消除日期不是最大值的所有记录

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# 

根据需要修改

于 2009-05-19T18:55:33.257 回答
0

我会尝试这样的事情:

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
于 2009-05-19T19:13:31.150 回答
0
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>
于 2009-05-19T20:29:00.730 回答
0

我不确定INNER JOIN这次指定“”意味着什么。其他答案使用子查询,所以这是一个使用两个INNER JOINs 且没有子查询的示例。而不是让我的头脑围绕您的架构:) 我使用 Northwind 来返回客户和他们最近订单的日期:

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);
于 2009-05-20T08:28:34.380 回答