【问题标题】:Athena inset into new value error : extraneous input 'INSERT' expecting {'(', ',', 'SELECT', 'VALUES', 'TABLE'}Athena 插入新值错误:无关输入 'INSERT' 期望 {'(', ',', 'SELECT', 'VALUES', 'TABLE'}
【发布时间】:2020-10-20 22:22:11
【问题描述】:

user_db 架构是这样的:

  2020-03  11         80     121
  2020-04  20         30     121
  2020-05  13         99     121
  2020-06  12         19     121
......    

老用户数据库是这样的:

month,user_count, total, grand_total, percentage_old, total_percentage_old
....
2020-06    20       30      121       73.3%           18.1%


需要在 Athena 查询中从 6 月更新值百分比、total_percentage 和旧数据数据库记录 percent_old、total_percentage_old,

with user_db as (
select *, user_count/total*100 as percentage, user_count/grand_total*100 as total_percentage
)

INSERT INTO  user_db (percentage,total_percentage) 
select percentage_old, total_percentage_old
from old_user_db  as u
where u.month = '2020-06'  

但出现此错误:

外部输入 'INSERT' 期望 {'(', ',', 'SELECT', 'VALUES', '表'}

需要保留 with 子句以便在其他查询中重复使用。 期望使用旧用户 db 值获得具有 6 月百分比和 total_percentage 的 user_db 表。

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • Athena 不支持向现有行添加字段。 INSERT INTO 将始终添加新行。

标签: sql amazon-athena


【解决方案1】:

我认为这不是错误的原因,因为该错误是语法错误,但您尝试插入 CTE,但无论如何都无法正常工作。

WITH user_db (…) 定义了一个临时关系user_db,它只在查询执行期间存在。如果您已经有一个名为user_db 的表,它将被这种关系所掩盖。 INSERT INTO user_db 无法插入此关系,因为它确实不存在。

user_db 关系也没有 FROM 子句,这是不受支持的,Athena 不会知道 user_counttotalgrand_total 列的来源。

不知道其他数据库能不能写这样的insert语句,没见过。无论哪种方式,Athena 都不支持它。如果您想了解有关支持的更多信息,请查看 INSERT INTO 文档。

我怀疑你正在尝试做这样的事情,但我不完全确定:

INSERT INTO user_db (month, user_count, percentage, total_percentage) 
SELECT
  month,
  user_count,
  user_count/total * 100,
  user_count/grand_total * 100
FROM old_user_db
WHERE month = '2020-06'

如果您需要更详细的帮助,您必须提供更多信息。

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多