我正在尝试使用C#中的SQLce(版本3.5)数据库中的生效日期进行过滤。这是我的查询:
SELECT FirstName, LastName, HomeID, ConditionID, ADate, OwnerID
FROM Sys_HomeOwner
WHERE (ADate =
(SELECT MAX(ADate) AS Expr1
FROM Sys_HomeOwner AS Sys_HomeOwner_1))
返回的错误是:
解析查询时出错。 (标记行号= 1,标记行 offset = 118,令牌错误=选择]
我一直在做一些查询,似乎可以在SQLCE中进行子选择。如果我拉出子选择,那么无论如何拉动最大日期,它对于整个表运行正常。
我做错了什么?
感谢您的帮助。
答案 0 :(得分:0)
你提到每行最多,我认为你需要使用GROUP BY
。
怎么样:
SELECT FirstName, LastName, NameID, ConditionID, ADate, OwnerID
FROM Sys_HomeOwner h
LEFT OUTER JOIN (SELECT MAX(ADate) AS maxdate, HomeID
FROM Sys_HomeOwner
GROUP BY HomeID) AS effectiveDates
ON h.HomeID = effectiveDates.HomeID
WHERE h.ADate = effectiveDates.maxdate
假设您想获取每个HomeID的最新日期
答案 1 :(得分:-1)
SELECT TOP 1 FirstName, LastName, HomeID, ConditionID, ADate, OwnerID
FROM Sys_HomeOwner
ORDER BY ADate DESC