首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ms Access - Join 2表提供重复记录

Ms Access - Join 2表提供重复记录
EN

Stack Overflow用户
提问于 2015-07-24 17:02:04
回答 2查看 27关注 0票数 0

我的查询有什么问题?我想加入2个表,并获得每个冷藏箱的最后记录,其中的GPSAlertType=“温度”。

冷藏表:

代码语言:javascript
复制
ReeferNo    TransporterName   Status
  R-1            ABC           Empty
  R-2            ABC           Empty
  R-3            ABC           Loaded
  R-4            ABC           Empty

FilteredAlerts表:

代码语言:javascript
复制
ReeferNo     AlertDateTime  ReceivedDateTime    GPSAlertType    Temperature   Location
  R-1        23/07/15 06:00   7/23/15 6:03      Temperature      7.05        Warehouse
  R-2        23/07/15 06:02   7/23/15 6:05      Arrival                      Warehouse
  R-3        24/07/15 10:37   7/24/15 10:39     Temperature      5.81        Store
  R-4        24/07/15 10:39   7/24/15 10:41     Departure                    Warehouse

结果应该是:

代码语言:javascript
复制
ReeferNo    TransporterName  Status AlertDateTime   ReceivedDateTime    GPSAlertType    Temperature  Location
 R-1            ABC          Empty  23/07/15 06:00  7/23/15 6:03        Temperature         7.05     Warehouse
 R-2            ABC          Empty                  
 R-3            ABC          Loaded 24/07/15 10:37  7/24/15 10:39       Temperature         5.81     Store
 R-4            ABC          Empty                  

我正在使用下面的查询,但它给了我重复的ReeferNo。

代码语言:javascript
复制
SELECT DISTINCT 
       r.ReeferNo, 
       r.TransporterName, 
       r.Status,
       sub2.AlertDateTime, 
       sub2.ReceivedDateTime, 
       sub2.GPSAlertType, 
       sub2.Temperature, 
       sub2.Location
FROM 
       Reefers AS r 
       LEFT JOIN 
       (
            SELECT DISTINCT 
                   sub1.ReeferNo, 
                   a2.AlertDateTime, 
                   a2.ReceivedDateTime, 
                   a2.GPSAlertType, 
                   a2.Temperature, 
                   a2.Location 
            FROM 
                   (
                       SELECT 
                              a1.ReeferNo, 
                              Max(a1.AlertDateTime) AS MaxOfAlertDateTime 
                       FROM FilteredAlerts AS a1 
                       WHERE a1.GPSAlertType='Temperature' 
                       GROUP BY a1.ReeferNo
                    ) AS sub1 
                    INNER JOIN FilteredAlerts AS a2 
                    ON 
                             (sub1.MaxOfAlertDateTime=a2.AlertDateTime) 
                         AND    (sub1.ReeferNo=a2.ReeferNo)
                    )  AS sub2 
ON r.ReeferNo = sub2.ReeferNo;

已有查询结果:

代码语言:javascript
复制
ReeferNo    TransporterName  Status   AlertDateTime    ReceivedDateTime GPSAlertType  Temperature  Location
R-1            ABC            Empty   23/07/15 06:00    7/23/15 6:03    Temperature     7.05        Warehouse
R-2            ABC            Empty             
R-3            ABC                    24/07/15 10:37    7/24/15 10:39   Temperature     5.81        Store
R-3            ABC            Loaded  24/07/15 10:37    7/24/15 10:39   Temperature     5.81        Store
R-4            ABC            Empty                 

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

发布于 2015-07-24 17:15:30

尝试一下,简单地将where子句与内连接查询一起使用

代码语言:javascript
复制
SELECT DISTINCT 
   r.ReeferNo, 
   r.TransporterName, 
   r.Status,
   sub2.AlertDateTime, 
   sub2.ReceivedDateTime, 
   sub2.GPSAlertType, 
   sub2.Temperature, 
   sub2.Location
FROM 
   Reefers AS r 
   INNER JOIN FilteredAlerts AS sub2
   ON r.ReeferNo = sub2.ReeferNo
   AND sub2.GPSAlertType='Temperature' 
票数 0
EN

Stack Overflow用户

发布于 2015-07-24 19:18:59

问题出在我的数据库里。我发现有重复记录的reeferno。我删除了副本,现在它可以正常工作了。

很抱歉。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31606345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档