Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用不同的日期字段联接表_Sql_Join_Netezza - Fatal编程技术网

Sql 使用不同的日期字段联接表

Sql 使用不同的日期字段联接表,sql,join,netezza,Sql,Join,Netezza,我正在尝试连接具有不同日期字段的表,以便按时间顺序对其进行对齐 我的桌子看起来像: 表1: id date_1 val -------------------------- 1 08/20/2018 0.4 1 08/21/2018 0.4 1 08/22/2018 0.5 1 10/05/2018 0.8 2 01/05/2019 0.1 2

我正在尝试连接具有不同日期字段的表,以便按时间顺序对其进行对齐

我的桌子看起来像:

表1:

id     date_1        val
--------------------------
1     08/20/2018     0.4   
1     08/21/2018     0.4   
1     08/22/2018     0.5  
1     10/05/2018     0.8           
2     01/05/2019     0.1   
2     02/10/2019     0.4   
2     02/11/2019     0.3   
表2:

id     date_2       total
---------------------------
1     08/18/2018    31.1    
1     08/19/2018    31.12    
1     08/22/2018    32.08   
1     08/23/2018    31.5            
2     03/01/2019    22.7    
如果可能,我希望加入这些表,并获得:

id     date_1        val       date_2       total
-----------------------------------------------------
1       null        null     08/18/2018     31.1
1       null        null     08/19/2018     31.12
1     08/20/2018     0.4        null        null
1     08/21/2018     0.4        null        null 
1     08/22/2018     0.5     08/22/2018     32.08
1     10/05/2018     0.8        null        null     
2     01/05/2019     0.1        null        null
2     02/10/2019     0.4        null        null
2     02/11/2019     0.3        null        null
2       null        null      03/01/2019    22.7

因为几乎没有日期重叠,我不知道怎么做。。。我只讨论了左连接,其中“left”表包含“right”表中的日期。有人知道如何做到这一点吗?

我想你想要一个
完全加入

select coalesce(t1.id, t2.id) as id, t1.date_1,
       t1.val, t2.date_2, t2.total
from table1 t1 full join
     table2 t2
     on t1.id = t2.id and t1.date_1 = t2.date_2

@jarlh是正确的,不过您必须在其中一个表中为id字段别名。你想要

SELECT
   *
FROM
  Table_1
FULL OUTER JOIN
   Table_2
      ON Table_1.id = Table_2.id t2id
;

看起来像一个
完全外部联接