gpt4 book ai didi

sql - Postgres :Get the latest record from a table

转载 作者:行者123 更新时间:2023-11-29 13:34:08 25 4
gpt4 key购买 nike

我有两个用于获取数据的表,我想从这些数据中识别与特定值最新相关的记录。我的第一张 table 是:

g_190048(
id,
logger_timestamp,
message_id,
begin_calc_timestamp ,
intersection_id,
matched_nr ,
distance ,
remainingphase_time ,
current_phase ,
advice_state,
advice ,
mode,
CONSTRAINT g_190048_pk
PRIMARY KEY (logger_timestamp , id )
)

"1001";1370253333921;190048;1546516;100;1;8028;36;1;0;-100;1
"1001";1370253334093;190048;1546719;100;1;7885;36;1;0;-100;1
"1001";1370253334593;190048;1547235;100;1;7885;36;1;0;-100;1
"1001";1370253334875;190048;1547516;100;1;7733;36;1;0;-100;1
"1001";1370253334906;190048;1547547;100;1;6631;35;1;0;-100;1
"1001";1370253335406;190048;1548047;100;1;6631;35;1;0;-100;1
"1001";1370253335687;190048;1548328;100;1;6306;35;1;0;-100;1
"1001";1370253335906;190048;1548532;100;1;5791;34;1;0;-100;1
"1001";1370253336234;190048;1548875;100;1;5518;34;1;0;-100;1
"1001";1370253336578;190048;1549157;100;1;5349;34;1;0;-100;1

第二张表:

g_190049 (
id,
logger_timestamp,
message_id,
begin_calc_timestamp,
msg1_recd_timestamp,
msg2_recd_timestamp,
msg1_end_timestamp,
msg2_end_timestamp
)

"1001";1370253333921;190049;1546516;1546516;1546516;1546578;1546578;
"1001";1370253334093;190049;1546719;1546719;1546719;1546735;1546750;
"1001";1370253334593;190049;1547235;1547235;1547235;1547250;1547250;
"1001";1370253334890;190049;1547516;1547516;1547516;1547532;1547532;
"1001";1370253334921;190049;1547547;1547547;1547547;1547563;1547563;
"1001";1370253335421;190049;1548047;1548047;1548047;1548063;1548063;
"1001";1370253335703;190049;1548328;1548328;1548328;1548344;1548344;
"1001";1370253335906;190049;1548532;1548532;1548532;1548563;1548563;
"1001";1370253336250;190049;1548875;1548875;1548875;1548891;1548891;
"1001";1370253336578;190049;1549157;1549157;1549157;1549235;1549235;

我有一个查询,它连接两个表并返回 msg1_recd_timestamp 和 remainingphase_timestamp

查询 1:

SELECT
g_190049.logger_timestamp,
g_190049.msg1_recd_timestamp,
g_190048.distance ,
g_190048.remainingphase_time,
g_190048.current_phase
FROM g_190049 g_190049, g_190048 g_190048
WHERE
g_190049.id = g_190048.id
AND g_190049.begin_calc_timestamp = g_190048.begin_calc_timestamp
AND g_190048.intersection_id = 100
and g_190048.matched_nr = 1
and g_190049.logger_timestamp BETWEEN '1370246100000' AND '1370253364000')

这个输出给了我:

    1370253333921   1546516  8028 36 1
1370253334093 1546719 7885 36 1
1370253334593 1547235 7885 36 1
1370253334890 1547516 7733 36 1
1370253334921 1547547 6631 35 1
1370253335421 1548047 6631 35 1
1370253335703 1548328 6306 35 1
1370253335906 1548532 5791 34 1
1370253336250 1548875 5518 34 1
1370253336578 1549157 5349 34 1
1370253336859 1549500 5167 34 1

但我想要的是,remainingphase_timestamp 的最新行

    1370253334890   1547516  7733 36 1
1370253335703 1548328 6306 35 1
1370253336859 1549500 5167 34 1

如果我在表中有 query1 的输出,我可以使用

实现我的目标
SELECT *
from TEMPO
where logger_timestamp IN (
select max(logger_timestamp)
from TEMPO
Group by remaining_time
)

但我不知道如何整合这两个查询。

最佳答案

select distinct on (remainingphase_time)
g_190049.logger_timestamp,
g_190049.msg1_recd_timestamp,
g_190048.distance ,
g_190048.remainingphase_time,
g_190048.current_phase
from
g_190049
inner join
g_190048 using(id, begin_calc_timestamp)
where
g_190048.intersection_id = 100
and g_190048.matched_nr = 1
and g_190049.logger_timestamp between '1370246100000' and '1370253364000'
order by remainingphase_time, g_190049.logger_timestamp desc

关于sql - Postgres :Get the latest record from a table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com