sql—通过扩展(并连接)另一个表中的结构来创建包含列的表

bejyjqdl  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(311)

table1 具有以下结构:

DESCRIBE table1;
+-------------+------------------------------------------------------------------------+
|  col_name   |                              data_type                                 |
+-------------+------------------------------------------------------------------------+
| key         | string                                                                 |
| source      | string                                                                 |
| address     | struct<address:string,zip:string,city:string,state:string,cntry:string>|
| column4     | date                                                                   |
| column5     | date                                                                   | 
+-------------+------------------------------------------------------------------------+

前两行 table1 是:

SELECT * FROM db.table1 limit 2;
+------+----------+-------------------------------------------------------------------------------------+--------------------+------------+--+
| key  | source   |                                         address                                     | column4            | column5    |
+------+----------+-------------------------------------------------------------------------------------+--------------------+------------+--+
| 001  | internet | {"address":" 123 FAKE ST","zip":"12345","city":"MIAMI","state":"FL","cntry":"USA"}  | 2007-01-27         | 2009-12-12 |
| 002  | internet | {"address":" 234 FAKE ST","zip":"23456","city":"MIAMI","state":"FL","cntry":"USA"}  | 2012-03-23         | 2014-01-30 |
+------+----------+-------------------------------------------------------------------------------------+--------------------+------------+--+

我想创建一个新的 table2 其中我复制了 table1 的信息,但在 address 列为五个组成列( address , zip , city , state , cntry )然后生成一个列,它是 address 以及 zip .
我试过:

CREATE TABLE table2 AS 
(
    SELECT 
           key, source, 
           address.address, address.zip, address.city, address.state, address.cntry,
           CONCAT(address.address, ' ', address.zip), 
           column4, column5 
    FROM 
           db.table1
);

但这给了我一个错误
Error: Error while compiling statement: FAILED: ParseException line 1:35 cannot recognize input near '(' 'select' 'key' in select clause (state=42000,code=40000) table2 应具有以下结构:

DESCRIBE table2;
+-------------+------------------------------------------------------------------------+
|  col_name   |                              data_type                                 |
+-------------+------------------------------------------------------------------------+
| key         | string                                                                 |
| source      | string                                                                 |
| address     | string                                                                 |
| zip         | string                                                                 |
| city        | string                                                                 |
| state       | string                                                                 |
| cntry       | string                                                                 |
| add_zip     | string                                                                 |
| column4     | date                                                                   |
| column5     | date                                                                   | 
+-------------+------------------------------------------------------------------------+

并包含来自 table1 .
坦率地说,我不确定这是否是拆分这个结构的正确方法,所以任何帮助都是非常有用的。

qyzbxkaa

qyzbxkaa1#

key 是列的错误名称,因为它是sql关键字。显然,括号是问题的根源。此外,您还缺少计算列的列名。
这有用吗?

CREATE TABLE table2 AS 
    SELECT key, source, 
           address.address, address.zip, address.city, address.state, address.cntry,
           CONCAT(address.address, ' ', address.zip) as address_zip, 
           column4, column5 
    FROM db.table1;

相关问题