2017-01-09 111 views
0

我使用的是pgsql 9.3。 我有兩張相同的表格。一個是主表,另一個是臨時表。每次進行更新時,如果數據不在主服務器中,則會將數據寫入臨時表中,從而將數據寫入臨時表中。

如果temp中只有一個條目,那麼我會得到所需的結果,即如果該條目不在master中,則它被插入。但是,如果我在臨時有多行,我的邏輯失敗。任何人都可以幫助我解決這個問題。
下面是我執行

如何在pgsql查詢結果中得到多行

select type, number, id, max(time) from testt t 
     where not exists (
      select 1 from testm m 
        where (
        m.id = t.id and m.type = t.type and m.number = t.number 
        and 
        ( 
         m.time = (select max(m1.time) from testm m1 where m.id=m1.id 
        ) 
       ) 
       ) 
      ) 
group by id, type, number; 

任何幫助表示讚賞stataemnt。提前致謝。

編輯:

數據在主表中:

 type |  number |    id |  time 
--------------+---------------+-----------------+--------------- 
    35817804 |  158014 | 262017400998389 | 1483336800000 
    35817804 |  158024 | 262017400998389 | 1483337400000 
    35817804 |  158014 | 262017400998389 | 1483338000000 
    35817804 |  158024 | 262017400998389 | 1483338900000 


數據在臨時表中:

type |  number |    id |  time 
---------+---------------+-----------------+--------------- 
35817804 |  158014 | 262017400998389 | 1483339500000 
35817804 |  158024 | 262017400998389 | 1483340100000 


在溫度,時間是針對行和這些組合都被不同不在主人面前。因此,既要插入製版師傅如下

type |  number |    id |  time 
---------+---------------+-----------------+--------------- 
35817804 |  158014 | 262017400998389 | 1483336800000 
35817804 |  158024 | 262017400998389 | 1483337400000 
35817804 |  158014 | 262017400998389 | 1483338000000 
35817804 |  158024 | 262017400998389 | 1483338900000 
35817804 |  158014 | 262017400998389 | 1483339500000 
35817804 |  158024 | 262017400998389 | 1483340100000 

但在運行上述查詢我的主人填充了以下數據,丟失的最後一項

type |  number |    id |  time 
---------+---------------+-----------------+--------------- 
35817804 |  158014 | 262017400998389 | 1483336800000 
35817804 |  158024 | 262017400998389 | 1483337400000 
35817804 |  158014 | 262017400998389 | 1483338000000 
35817804 |  158024 | 262017400998389 | 1483338900000 
35817804 |  158014 | 262017400998389 | 1483339500000 


編輯後:
以上聲明被稱爲在一個程序中增加查詢主表的響應

+0

請** [編輯] **你的問題,並添加一些示例數據以及基於該數據的預期輸出。 [**格式化文本**](http://stackoverflow.com/help/formatting)請,[無屏幕截圖](http://meta.stackoverflow.com/questions/285551/why-may-i-not上傳圖像的代碼的時候問一個問題/ 285557#285557) –

+0

注意:沒有什麼插入您的查詢。你想添加新的記錄到主表嗎?那麼你將需要一個插入語句。 – wildplasser

+0

上面的語句在插入主表中輸出的過程中調用,對不起,我沒有提到它。 – user2261968

回答

0

我已經設置了rextester例子:http://rextester.com/TJLG3973

我使用了一個CTE查詢來避免在哪裏條件下的select max(time)。與where m.type is null一起使用的LEFT JOIN將返回不存在的記錄。

with trecords as 
(
    select t.type, t.number, t.id, max(t.time) as time 
    from testt t 
    group by t.type, t.number, t.id 
) 
insert into testm 
select x.type, x.number, x.id, x.time 
from trecords x 
    left join testm m 
    on m.type = x.type 
     and m.number = x.number 
     and m.id = x.id 
     and m.time = x.time 
where m.type is null 
; 

我已經添加了兩行到臨時表,只是爲了檢查最大(時間)工作正常。

create temp table testt (type int, number int, id bigint, time bigint); 
insert into testt values 
(35817804, 158014, 262017400998389, 1483339500000), 
(35817804, 158014, 262017400998389, 1483339600000), 
(35817804, 158014, 262017400998389, 1483339700000), 
(35817804, 158024, 262017400998389, 1483340100000); 

而且隨着問題提供的主表:

create temp table testm (type int, number int, id bigint, time bigint); 
insert into testm values 
(35817804, 158014, 262017400998389, 1483336800000), 
(35817804, 158024, 262017400998389, 1483337400000), 
(35817804, 158014, 262017400998389, 1483338000000), 
(35817804, 158024, 262017400998389, 1483338900000); 

這是最後的結果是:

+----------+--------+-----------------+---------------+ 
| type | number |  id  |  time  | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158014 | 262017400998389 | 1483336800000 | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158024 | 262017400998389 | 1483337400000 | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158014 | 262017400998389 | 1483338000000 | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158024 | 262017400998389 | 1483338900000 | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158024 | 262017400998389 | 1483340100000 | 
+----------+--------+-----------------+---------------+ 
| 35817804 | 158014 | 262017400998389 | 1483339700000 | 
+----------+--------+-----------------+---------------+