我有一个包含StormID列和ModelBaseTime列的表(以及其他一些列)..
我试图获取StormID =' AL012015'的所有记录和所有字段。并且只有那些具有针对该特定StormID的最新ModelBaseTime的记录。
这是我通过研究SO所尝试的。
SELECT * FROM adeck_raw WHERE StormID = 'AL012015'
AND ModelBaseTime IN(SELECT MAX(ModelBaseTime))
ORDER BY StormID;
^^这会使用多个ModelBaseTime而不是最新的ModelBaseTime返回太多结果。
SELECT * FROM adeck_raw
WHERE StormID = 'AL012015' AND ModelBaseTime IN
(SELECT MAX(ModelBaseTime)
FROM adeck_raw
GROUP BY StormID
)
GROUP BY StormID
ORDER BY ModelBaseTime;
^^这会返回1条记录(我知道有多条记录)
SELECT * adeck_raw AS t
CROSS JOIN (
SELECT StormID, MAX(ModelBaseTime) AS latest_date FROM adeck_raw WHERE StormdID = 'AL012015' GROUP BY StormID
) AS sq
USING (StormID, ModelBaseTime);
^^这不执行... sql语法错误
SELECT*, MAX(ModelBaseTime)
FROM adeck_raw WHERE StormID = 'AL012015'
GROUP BY StormID;
^^这只返回1条记录
感谢您的任何建议和示例。 布赖恩
答案 0 :(得分:1)
试试这个:
SELECT t1.*
FROM adeck_raw AS t1
INNER JOIN (
SELECT MAX(ModelBaseTime) AS latest_date
FROM adeck_raw
WHERE StormID = 'AL012015'
) AS t2 ON t1.ModelBaseTime = t2.latest_date
WHERE StormID = 'AL012015';