我想使用两个sql脚本在bigquery中使用 bq query
命令。以下是两个脚本:
1-创建表
EXECUTE IMMEDIATE
'''CREATE TABLE IF NOT EXISTS dataset.newtable (
id STRING, ''' ||
(SELECT STRING_AGG(
segment ||
" INT64 " ORDER BY segment
) FROM (
SELECT DISTINCT segment FROM dataset.source
)
) ||
''')''';
2-更新表
EXECUTE IMMEDIATE
'''INSERT dataset.newtable (
SELECT id, ''' ||
( SELECT STRING_AGG("COUNTIF(
segment = '" ||
segment ||
"') AS " ||
segment ORDER BY segment
) FROM (
SELECT DISTINCT segment FROM dataset.source
) ) ||
''' FROM dataset.source GROUP BY 1 ORDER BY 1
)''';
将这两个脚本用作 bq query
命令,我有个错误:
Syntax error: Expected keyword IMMEDIATE but got to end of the script at [1:8]
还有别的办法吗?
谢谢您
1条答案
按热度按时间8ljdwjyq1#
中的默认查询方言
bq
命令行工具是遗留的sql。您需要切换到标准sql方言才能运行脚本:--use_legacy_sql=false
或者--nouse_legacy_sql
标记到命令行语句。在sql前面加上前缀
#standardSQL
也可以将标准sql设置为默认值。看这里。