SELECT 第二高的时间戳和相关列
SELECT second highest timestamp and correlative columns
在一个 table 包中,每个包都有一个位置列和时间列,我可以通过以下查询获得最近的位置:
SELECT b.*
FROM bag_position b
WHERE time = (
SELECT MAX(c.time)
FROM bag_position c
WHERE c.bag_id = b.bag_id
);
如何获得第二高的位置?我试过这个,但它不起作用:
SELECT b.*
from bag_position b
where time = ( select max(c.time) from bag_position c where c.time < (select max d.time from bag_position d) and c.bag_id = b.bag_id );
您可以使用 offset
和 limit
代替 max()
:
select b.*
from bag_position b
where b.time = (select b2.time
from bag_position b2
where b2.bag_id = b.bag_id
order by time desc
limit 1 offset 1
);
在一个 table 包中,每个包都有一个位置列和时间列,我可以通过以下查询获得最近的位置:
SELECT b.*
FROM bag_position b
WHERE time = (
SELECT MAX(c.time)
FROM bag_position c
WHERE c.bag_id = b.bag_id
);
如何获得第二高的位置?我试过这个,但它不起作用:
SELECT b.*
from bag_position b
where time = ( select max(c.time) from bag_position c where c.time < (select max d.time from bag_position d) and c.bag_id = b.bag_id );
您可以使用 offset
和 limit
代替 max()
:
select b.*
from bag_position b
where b.time = (select b2.time
from bag_position b2
where b2.bag_id = b.bag_id
order by time desc
limit 1 offset 1
);