Access Query中的问题使用max()获取唯一值

时间:2016-10-29 18:55:45

标签: sql ms-access

我有一个查询从两个表中提取信息,并列出OrderNumber字段中每个唯一值的最近添加的条目。我认为它有效但我必须改变一些东西,因为它现在显示的是所有内容,而不是每个订单号的最新内容:

*请注意,日期实际上也会显示时间,但是当我将其粘贴到Stack Overflow中时它会发生变化。

这就是我想要显示数据的方式:

Job    Order Number Spool                   Status  Location    Modified
43400011    1371246 00040                   00721   DEASLEY     29-Oct-16
43400011    1371248 00042                   00721   DEASLEY     29-Oct-16
43400011    1386582 00059                   00721   CPERRY      29-Oct-16
43400011    1386584 00061                   00721   DEASLEY     29-Oct-16
43400011    1405367 00194                   00721   DEASLEY     29-Oct-16
61400046    1432157 00329                   00661   tsanchez    17-Oct-16
61400046    1432158 00330                   00661   tsanchez    17-Oct-16

但它反而出现了:

Job    Order Number Spool                   Status  Location    Modified
43400011    1371246 00040                   00721   DEASLEY     29-Oct-16
43400011    1371248 00042                   00721   DEASLEY     29-Oct-16
43400011    1371248 00042                   00721   DEASLEY     29-Oct-16
43400011    1386582 00059                   00721   CPERRY      29-Oct-16
43400011    1386584 00061                   00721   DEASLEY     29-Oct-16
43400011    1405367 00194                   00721   DEASLEY     29-Oct-16
61400046    1432157 00329                   00661   tsanchez    17-Oct-16
61400046    1432158 00330                   00661   JIFFY       29-Oct-16
61400046    1432158 00330                   00661   DEASLEY     29-Oct-16
61400046    1432158 00330                   00661   tsanchez    17-Oct-16

我的查询:

SELECT [Branch/plant] AS Job, SpoolReport.OrderNumber AS [Order Number], 
       [Spl #] AS Spool, [Comp#] AS Status, SpoolReport.Location, 
       SpoolReport.Modified
FROM SpoolReport, STATUS
WHERE (SpoolReport.OrderNumber = [STATUS]![Order #]) 
AND   (((SpoolReport.Modified) IN
           (SELECT Max(SpoolReport.Modified) 
            FROM SpoolReport 
            GROUP BY SpoolReport.OrderNumber)) 
AND   ((SpoolReport.Complete)=False));

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

考虑将子查询与外部主查询相关联。现在,WHERE子句会查看GROUP BY查询中与行&#39> OrderNumber 不匹配的任何日期。下面将子查询等同于外部查询的 OrderNumber 。请注意使用表别名 main sub 来组织查询的相关性:

SELECT main.[Branch/plant] AS Job, main.OrderNumber AS [Order Number], 
       main.[Spl #] AS Spool, main.[Comp#] AS Status, main.Location, 
       main.Modified
FROM SpoolReport main
INNER JOIN STATUS s 
    ON main.OrderNumber = s.[Order #]
WHERE ((main.Modified = 
           (SELECT Max(sub.Modified) 
            FROM SpoolReport sub
            WHERE sub.OrderNumber = main.OrderNumber)) 
   AND (main.Complete = False)); 

更好的是,考虑将聚合查询作为派生表(或保存的查询)加入:

SELECT main.[Branch/plant] AS Job, main.OrderNumber AS [Order Number], 
       main.[Spl #] AS Spool, main.[Comp#] AS Status, main.Location, 
       main.Modified
FROM (SpoolReport main
INNER JOIN STATUS s 
    ON main.OrderNumber = s.[Order #])
INNER JOIN 
        (SELECT sub.OrderNumber, Max(sub.Modified) As MaxModified
         FROM SpoolReport sub
         GROUP BY sub.OrderNumber) As dT
    ON main.OrderNumber = dT.OrderNumber
    AND main.Modified = dT.MaxModified
WHERE (main.Complete = False);