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

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

table1 具有以下结构:

  1. DESCRIBE table1;
  2. +-------------+------------------------------------------------------------------------+
  3. | col_name | data_type |
  4. +-------------+------------------------------------------------------------------------+
  5. | key | string |
  6. | source | string |
  7. | address | struct<address:string,zip:string,city:string,state:string,cntry:string>|
  8. | column4 | date |
  9. | column5 | date |
  10. +-------------+------------------------------------------------------------------------+

前两行 table1 是:

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

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

  1. CREATE TABLE table2 AS
  2. (
  3. SELECT
  4. key, source,
  5. address.address, address.zip, address.city, address.state, address.cntry,
  6. CONCAT(address.address, ' ', address.zip),
  7. column4, column5
  8. FROM
  9. db.table1
  10. );

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

  1. DESCRIBE table2;
  2. +-------------+------------------------------------------------------------------------+
  3. | col_name | data_type |
  4. +-------------+------------------------------------------------------------------------+
  5. | key | string |
  6. | source | string |
  7. | address | string |
  8. | zip | string |
  9. | city | string |
  10. | state | string |
  11. | cntry | string |
  12. | add_zip | string |
  13. | column4 | date |
  14. | column5 | date |
  15. +-------------+------------------------------------------------------------------------+

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

qyzbxkaa

qyzbxkaa1#

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

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

相关问题