简体   繁体   English

MS Access / SQL子查询的语法,包括聚合函数

[英]Syntax of MS Access/SQL sub-query including aggregate functions

I am trying to produce a database to manage maintenance of equipment. 我试图建立一个数据库来管理设备维护。 I have two tables: 我有两个表:

  1. One ( Inventory ) containing details of each piece of equipment, including Purchase Date and Service Period , 一个( 库存 ),其中包含每台设备的详细信息,包括购买日期服务期限
  2. One containing details of work done ( WorkDone ), including the date the work was carried out ( Work Date ). 其中一个包含已完成工作的详细信息( WorkDone ),包括完成工作的日期Work Date )。

I would like a query that displays the date that it should be next serviced. 我想要一个显示下一次维修日期的查询。 So far I have: 到目前为止,我有:

SELECT Max(DateAdd('m', [Inventory].[Service Period], 
                        [WorkDone].[Work Date])) AS NextServiceDate, 
       Inventory.Equipement
FROM Inventory INNER JOIN WorkDone ON Inventory.ID = WorkDone.Equipment
GROUP BY Inventory.Equipement

This works well as long as some work done has been registered for a given piece of equipment. 只要已为给定设备记录了已完成的某些工作,此方法就很好用。 If no work has been carried out I would like the NextServiceDat e to also show 如果未执行任何工作,我希望NextServiceDat e也显示

DateAdd('m',[Inventory].[Service Period], [Inventory].[Purchase Date])

However, I cannot work out how to get SQL/MS access to compare two values and only display the greater of the two. 但是,我不知道如何获取SQL / MS访问权限以比较两个值,而仅显示两个中较大的一个。 From reading around I think I should be able to do a sub-query, but I cannot work out how to phase it. 通过阅读,我认为我应该能够进行子查询,但是我无法弄清楚如何对其进行阶段化。

I've been trying to adapt @MikeTeeVee's answer from here: Is there a Max function in SQL Server that takes two values like Math.Max in .NET? 我一直在尝试从此处适应@MikeTeeVee的回答: SQL Server中是否有一个Max函数接受两个值,如.NET中的Math.Max? . But I keep getting errors saying that query is not part of an aggregate function and I'm not certain what I doing wrong. 但是我不断收到错误消息,说查询不是聚合函数的一部分,而且我不确定自己做错了什么。 For example, I tried: 例如,我尝试过:

SELECT Inventory.Equipement,
       (SELECT MAX(NSD_proxy) 
        FROM (VALUES 
             (Max(DateAdd('m', Inventory.[Service Period], WorkDone.[Work Date]))), 
                 (DateAdd('m', Inventory.[Service Period], Inventory.[Purchase Date]))) 
         AS FUNCTION(NSD_proxy)
        ) AS NextServiceDate,
FROM Inventory INNER JOIN WorkDone ON Inventory.ID = WorkDone.Equipment
GROUP BY Inventory.Equipement

which has some syntax error. 这有一些语法错误。

You don't have to compare the two dates, just check if a WorkDone record exists to match the Inventory record. 您不必比较两个日期,只需检查是否存在WorkDone记录与Inventory记录匹配即可。

You can use: 您可以使用:

IIF(ISNULL(WorkDone.Equipment),
DateAdd('m',[Inventory].[Service Period],[Inventory].[Purchase Date]),
Max(DateAdd('m',[Inventory].[Service Period],[WorkDone].[Work Date])))
AS NextServiceDate

The rest of your query can remain as is. 您查询的其余部分可以保持不变。

Consider a LEFT JOIN to return matched or unmatched records where latter is filled with NULLs, and then run your aggregate, MAX , with an NZ() : 考虑一个LEFT JOIN返回匹配或不匹配的记录,后者用NULL填充,然后使用NZ()运行聚合MAX

SELECT Max(NZ(DateAdd('m', i.[Service Period], w.[Work Date]),
              DateAdd('m', i.[Service Period], i.[Purchase Date]))
          ) AS NextServiceDate, i.Equipement
FROM Inventory i LEFT JOIN WorkDone w ON i.ID = w.Equipment
GROUP BY i.Equipement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM