[英]Merging two tables including the union on all values in a single column in Athena (Presto)
Here is table 1: 这是表1:
prof_id title id date0
1 Z 0 4/3
1 A 0 4/3
1 B 0 4/4
1 C 0 4/4
2 C 0 4/6
2 D 0 4/6
2 E 0 4/6
Here is table 2: 这是表2:
title id date1
A 0 1/2
C 0 1/2
D 0 1/3
E 0 1/3
H 0 1/5
J 0 1/6
I'd like to create a table like this: 我想创建一个这样的表:
prof_id date0 title id date1
1 4/3 Z 0 NA
1 4/3 A 0 1/2
1 4/3 B 0 NA
1 4/4 C 0 1/2
2 4/4 C 0 1/2
2 4/6 D 0 1/3
2 4/6 E 0 1/3
NA NA H 0 1/5
NA NA J 0 1/6
The column in the title in this case would be title. 在这种情况下,标题中的列将为title。 I'm pretty new to SQL so any help would be much appreciated.
我对SQL还是很陌生,所以任何帮助将不胜感激。
Note: I'm running this on Amazon Athena 注意:我正在Amazon Athena上运行它
You may try using a full outer join: 您可以尝试使用完全外部联接:
SELECT
t1.prof_id,
t1.date0,
COALESCE(t1.title, t2.title) AS title,
COALESCE(t1.id, t2.id) AS id,
t2.date1
FROM table1 t1
FULL OUTER JOIN table2 t2
ON t1.title = t2.title AND
t1.id = t2.id;
Try the following. 尝试以下方法。 This is ANSI so should work on any of the major DBMS:
这是ANSI,因此可以在任何主要DBMS上使用:
SELECT a.prof_id, a.date0, b.title, b.id, b.datet1
FROM [table 1] a
RIGHT JOIN [table 2] b ON a.title = b.title
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.