Postgresql连接表

时间:2014-12-04 22:43:09

标签: postgresql

拳头表:

Value Time
4     2014.12.2 14:00:00
5     2014.12.2 14:01:00
7     2014.12.2 14:02:00
4     2014.12.2 14:03:00

第二张表:

Value Time
10    2014.12.2 14:00:00
5     2014.12.2 14:01:00
3     2014.12.2 14:03:00
12    2014.12.2 14:05:00

怎么做:

Value, Value2 Time
4      10     2014.12.2 14:00:00
5      5      2014.12.2 14:01:00
7      3      2014.12.2 14:02:00
4      -      2014.12.2 14:03:00
-      12     2014.12.2 14:05:00

我已经实现了代码:

SELECT t1.value as Value,
                t2.value as Value2,
                COALESCE(t1.Time, t2.Time) AS Time
           FROM t1
FULL OUTER JOIN t2 USING (Time)

它是这样的:

Value, Value2 Time
-      20     2014.12.4 2:25:58
20     -      2014.12.4 2:25:58
20    212     2014.12.4 2:26:09
-     212     2014.12.4 2:26:19
-      12     2014.12.4 2:26:20

这里怎么做:

Value, Value2 Time
20    20      2014.12.4 2:25:58
20    212     2014.12.4 2:25:09
-     212     2014.12.4 2:26:19
-     12      2014.12.4 2:26:20

2 个答案:

答案 0 :(得分:1)

就像

一样简单
         SELECT t1.value as Value,
                t2.value as Value2,
                COALESCE(t1.Time, t2.Time) AS Time
           FROM t1
FULL OUTER JOIN t2 USING (Time)

参考文献:

答案 1 :(得分:0)

Select t1.value,
   t2.value Value2,
   Coalesce(t1.time, t2.time) 
From t1
Full outer join t2
   On t1.time = t2.time