我有一个关于从表中获取最新记录的经典问题。以下是声明:
SELECT t1.Item, t1.Price, t1.ODate
FROM Order AS t1
JOIN ( (SELECT Item, Price, Max(ODate) As MaxDate
FROM Order
GROUP BY Item ) As t2 )
ON t1.Item = t2.Item and t1.Odate = MaxDate
ORDER BY t1.Item ASC
此语句适用于phpMyAdmin和LibreOffice基础,但LibreOffice basic在SQL表达式中给出了一条消息:语法错误。
任何帮助?
答案 0 :(得分:0)
也许你应该从t2的选择中删除“Price”。这在加入中似乎是无稽之谈,并且会在MSSMS 2008中导致下面的错误消息,这在我的情况下。
列'Order.Price'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。
尝试
SELECT t1.Item, t1.Price, t1.ODate
FROM Order AS t1
JOIN (SELECT Item, Max(ODate) As MaxDate
FROM Order
GROUP BY Item ) As t2
ON t1.Item = t2.Item and t1.Odate = MaxDate
ORDER BY t1.Item ASC