[英]Postgresql: append two tables with different columns
I would like to append one table to the other; 我想将一张桌子附加到另一张桌子上。 both tables may have different columns.
两个表可能具有不同的列。 The result should be a table with all columns and where values do not exist, it should be a missing observation.
结果应该是一个包含所有列的表,并且在其中值不存在的地方,应该是缺少的观察值。 The data are time series - which I am getting from different sources due to time span constraints - so I need to "stack" them on each other, but it could be that one or the other column is added or dropped off.
数据是时间序列的-由于时间跨度的限制,我从不同的来源获取数据-因此我需要将它们彼此“堆叠”,但是可能是添加或删除了一个或另一列。
As there is a little overlap in the rows I am looking for a solution that would take the data of first table. 由于行中有一点重叠,因此我正在寻找一种解决方案,该方案将采用第一个表的数据。 The problem is then for those column not existing in table 1, they wouldn't exist either when I pick table 1 over table 2.
然后的问题是,对于表1中不存在的那些列,当我在表2中选择表1时,它们也不存在。
Current solution is to cut-off table 2 so there is no overlap. 当前的解决方案是截止表2,因此没有重叠。
table 1: 表格1:
date AA BB CC DD
20100101 9 10 11 12
20100102 10 11 12 13
table 2: 表2:
date AA BB CC EE FF
20100102 99 99 10
20100103 11 12 13 14 10
20100104 12 13 14 15 11
and the result should be 结果应该是
date AA BB CC DD EE FF
20100101 9 10 11 12
20100102 10 11 12 13 99 10
20100103 11 12 13 14 10
20100104 12 13 14 15 11
So I do not in fact have anything to "join" on as suggested here: SQL union of two tables with different columns 因此,我实际上没有按此处建议的方式“加入”任何东西: 具有不同列的两个表的SQL联合
coalesce
function may be used like in the following : coalesce
函数的用法如下:
select coalesce(t1.date,t2.date) date,
coalesce(t1.aa,t2.aa) aa,
coalesce(t1.bb,t2.bb) bb,
coalesce(t1.cc,t2.cc) cc,
t1.dd,
t2.ee,
t2.ff
from table1 t1 full outer join table2 t2 on ( t1.date = t2.date );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.