0

我有一个包含 3 列的表 - time_stamp,meter_id,value。所有meter_ids都是实时记录值.EX-:

    time_stamp      value   meter_id

06/14/13 12:17:00   1212    1
06/15/13 12:20:01   12      1
06/16/13 12:25:30   1213    2
06/17/13 12:30:12   12333   2
06/18/13 12:32:34   111111  3

我需要找到每个meter_id 的最新值。

编辑

最终输出应该是-:

time_stamp          value   meter_id

06/15/13 12:20:01   12        1
06/17/13 12:30:12   12333     2
06/18/13 12:32:34   111111    3

我希望这能帮助你理解我的问题。

谢谢

4

4 回答 4

1

可能你想要这样的东西吗?

select value, Meter_id from table group by Meter_id order by time_stamp desc 
于 2013-06-25T06:15:58.587 回答
0

如果我理解正确,您需要所有仪表 ID 的最新条目。您可以自行加入表格并使用group by

select
b.time_stamp, b.value, b.meter_id
from
  your_table as b
  inner join (
    select max(time_stamp) as recent_time, meter_id
    from your_table
    group by meter_id
  ) as view on ( view.meter_id = b.meter_id  and view.unique_key_from_your_table = b.unique_key_from_your_table)

如果您没有任何唯一键但时间戳。你可以time_stamp作为unique_key_from_your_table

所以加入条件看起来像

view.meter_id = b.meter_id and view.recent_time = b.time_stamp
于 2013-06-25T06:20:14.810 回答
0

尝试:

select r.*
from myTable r
join (select meter_id, max(time_stamp) max_time
      from myTable
      group by meter_id) m
  on r.meter_id = m.meter_id and r.time_stamp = m.max_time
于 2013-06-25T06:30:02.280 回答
0

我猜 time_stamp 是记录时间,如果是这样试试这个

SELECT MAX(time_stamp),meter_id,value FROM table GROUP BY meter_id
于 2013-06-25T06:18:13.473 回答