2013-06-18 108 views
1

我有兩張表格用於獲取數據,並從這些數據中查找與特定值最新相關的記錄。 我的第一個表是:Postgres:從表中獲取最新記錄

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 
) 

但我不知道如何整合這兩個查詢。

+0

查看'order by'和'limit'。和'加入'。 –

+0

試着這麼做...'選擇 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_190049.id = g_190048加入g_190048 g_190048 .ID WHERE g_190049.begin_calc_timestamp = g_190048.begin_calc_timestamp AND g_190048.intersection_id = 100 和g_190048.matched_nr = 1 和g_190049.logger_timestamp BETWEEN '1370246100000' AND '1370253364000');' –

+0

Anantha,其添加爲回答。 –

回答

1
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 
+0

謝謝你的答案 –