我是SQL的新手,我正在尝试创建一个将对表进行排序的查询(我已经使该部分工作),然后只显示每个相应组件和序列号的最新记录。
基本上我正在制作一个签入/签出系统,并希望仅列出每个特定组件的最新状态。每次签入和签出组件时,都会为该特定签入/签出发出唯一ID。因此,我知道我可以使用最高的唯一ID来确定在输出表中显示哪条记录。
(顶部图表是我所拥有的,底部是我想要的)
如何删除前两个字段重复的记录?
到目前为止我所拥有的:
SELECT [Check In/Out].Component, [Check In/Out].[Serial Number], [Check In/Out].[Unique ID], [Check In/Out].DateTime, [Check In/Out].Initials, [Check In/Out].[Check In/Out]
FROM [Check In/Out]
ORDER BY [Check In/Out].Component, [Check In/Out].[Serial Number], [Check In/Out].[Unique ID];
我找到了类似的代码(如下所示),但是我修改代码以使用我的尝试失败了:
SELECT Customers.ID, Customers.FullName, Customers.SaleDate
FROM Customers
WHERE [SaleDate] =
(SELECT Max([SaleDate])
FROM Customers AS X
WHERE X.[FullName] = [Customers].[FullName])
非常感谢任何帮助。
答案 0 :(得分:2)
对于Access SQL,这可能有效:
SELECT *
FROM [Check In/Out] AS tA INNER JOIN (SELECT Component, [Serial Number],
MAX(Unique ID) AS highest_id
FROM [Check In/Out]
GROUP BY Component, [Serial Number]) AS
tB ON tA.Component = tB.Component
AND
tA.[Serial Number] = tB.[Serial Number]
AND
tA.[Unique ID] = tB.highest_id
答案 1 :(得分:1)
如果您创建一个新查询并将其调用" MaxId":
SELECT [Check In/Out].Component, [Check In/Out].[Serial Number],
max ([Check In/Out].[Unique ID]) as max_id
FROM [Check In/Out]
group by [Check In/Out].Component, [Check In/Out].[Serial Number]
然后,您可以从新查询加入此查询:
SELECT
[Check In/Out].Component, [Check In/Out].[Serial Number],
[Check In/Out].[Unique ID], [Check In/Out].DateTime,
[Check In/Out].Intials, [Check In/Out].[Check In/Out]
FROM
[Check In/Out]
INNER JOIN MaxId ON
[Check In/Out].[Serial Number] = MaxId.[Serial Number] AND
[Check In/Out].Component = MaxId.Component
WHERE
[Check In/Out].[Unique ID] = [MaxId].[max_id];