0

我创建了一个按名称搜索事件的查询。结果列出同名事件的年份(2012 年 10 月 12 日至 2013 年 4 月 30 日)。我的问题是如何让我的查询只显示我想要选择的年份内具有相同名称的事件。

例如:我只想查看本月或本年同名的事件。

询问:

SELECT
    UserField03, GageNumber, GageType, LastCheckOutDate, CurrentLocation 
FROM 
    Gages
WHERE 
    GageNumber IN (SELECT DISTINCT Gages.GageNumber
                   FROM Gages 
                   INNER JOIN Events ON Gages.GageID = Events.GageID
                   WHERE (Name = 'Returned to Cal')
                  ) 
ORDER BY 
    LastCheckOutDate
4

1 回答 1

0

尝试这样的事情:

SELECT UserField03, GageNumber, GageType, LastCheckOutDate, CurrentLocation
FROM Gages INNER JOIN
(SELECT GageID FROM Events eo WHERE eo.Name 
    IN(SELECT e.Name FROM Events e WHERE e.DateOfEvent BETWEEN '2012-10-12' AND '2013-04-30' AND e.Name='Returned to Cal'
    GROUP BY e.Name HAVING COUNT(*) > 1) AND  e.DateOfEvent BETWEEN '2012-10-12' AND '2013-04-30')  EveAlias
ON Gages.GageID=EveAlias.GageID

在最嵌套的子选择中,您将获取事件的名称,它满足日期和名称条件并且其名称出现多次(HAVING COUNT(*) > 1)在定义的时间段内。在外部选择中,您正在获取GageId此类事件,因此您可以执行最终加入Gages

于 2013-05-16T22:56:58.133 回答