我有一个具有以下结构的表:
timestamp, tagid, value
我想要一个查询,我只能收到标签ID列表的最新时间戳值。例如:
SELECT * FROM floatTable WHERE tagid IN(1,2,3,4,5,6)
将返回整个值集。我只想要每个存储的最新值。
答案 0 :(得分:3)
select f.timestamp, f.tagid, f.value
from floatTable f
inner join (
select tagid, max(timestamp) as timestampMax
from floatTable
where tagid in (1,2,3,4,5,6)
group by tagid
) fm on f.tagid = fm.tagid and f.timestamp = fm.timestampMax
答案 1 :(得分:1)
这个怎么样:
select vt.tagid, maxdate, t.value
from
(
select tagid, max(timestamp) as maxdate
from floatTable
group by tagid
) vt
inner join floatTable t on t.tagid = vt.tagid
where t.timestamp = vt.maxdate
and t.tagid in (1,2,3,4,5,6)