我想通过以下查询从笔记中选择最后一个笔记:
SELECT cn.Note, MAX(cn.Date)
FROM CarsNote cn
WHERE CustomerID = '80' AND cn.Type = 'INFO'
GROUP BY Note
当我执行此查询时,我得到四个结果,但我只想要最后一个音符。
当我做SELECT TOP 1
时,我没有得到最新的说明。
这里缺少什么?
答案 0 :(得分:2)
您需要ORDER BY
:
SELECT TOP 1 cn.Note, cn.Date as maxdate
FROM CarsNote cn
WHERE CustomerID = '80' AND cn.Type = 'INFO'
GROUP BY cn.Note, cn.Date
ORDER BY maxdate desc;
答案 1 :(得分:2)
没有MAX或GROUP BY。它只是:
SELECT TOP 1 cn.Note, cn.Date
FROM CarsNote cn
WHERE cn.CustomerID = '80' AND cn.Type = 'INFO'
ORDER BY cn.Date desc;