首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用BQ命令执行bq查询时出现问题时的情况。

使用BQ命令执行bq查询时出现问题时的情况。
EN

Stack Overflow用户
提问于 2018-03-18 04:26:46
回答 3查看 581关注 0票数 0

我试图使用bq命令通过bash执行查询。通过web和app script @ script.google.com执行查询时都可以工作。但是,当作为一个简单的bash (或者仅仅通过cmd提示符)执行时,我一直在接收一个"Syntax error: Unexpected keyword THEN at [1:393]"。Col 393指的是案件的中间部分陈述时。我已经做了大量的搜索,并且找不出我做错了什么(因为查询在其他地方工作)。可能有人会对导致错误的原因提出建议吗?

代码语言:javascript
运行
复制
bq query --destination_table abcdefcloud:ds_tables.daily_over_frequency_output
    --replace --use_legacy_sql=false 'with freq as
    (select month as month,campaign_id,campaign,case when  freq = '1' then 'a'
     when freq = '2' then 'b'
     when freq = '3-6' then 'c'
     when freq = '7-9' then 'd'
     when freq = '10-19' then 'e'
     when freq = '20-29' then 'f'
     when freq = '30-39' then 'g'
     when freq = '40-49' then 'h'
     when freq = '50-59' then 'i'
     when freq = '60-69' then 'j'
     when freq = '70-79' then 'k'
     when freq = '80-89' then 'l'
     when freq = '90-99' then 'm'
     when freq = '100+' then 'n'
     else 'other' end as sort,
     freq,sum(imps) as imps,sum(uu) as uu from...

非常感谢你的帮助。布赖恩P

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-18 08:35:00

这里的问题是由于在查询中使用相同的单引号造成的,这些单引号与您要包装整个查询的引号相同。

你的选择:

  1. 转义查询when freq = \'1\' then \'a\'中的所有引号
  2. 将引号改为双引号when freq = "1" then "a"
  3. 通过一个文件将查询传递给bq查询命令。cat your_query.sql | bq query

选项3:是最优雅的解决方案,也有助于以一种很好的方式组织代码,将SQL保持为SQL,而不是将大量的SQL直接嵌入到bash代码中。您不需要对当前的SQL进行任何更改。

完整的示例如下所示

代码语言:javascript
运行
复制
cat your_query.sql | bq query \
  --destination_table abcdefcloud:ds_tables.daily_over_frequency_output \
  --replace \
  --use_legacy_sql=false 

其中your_query.sql将包含:

代码语言:javascript
运行
复制
with freq as
(select month as month,campaign_id,campaign,case when  freq = '1' then 'a'
 when freq = '2' then 'b'
 when freq = '3-6' then 'c'
 when freq = '7-9' then 'd'
 when freq = '10-19' then 'e'
 when freq = '20-29' then 'f'
 when freq = '30-39' then 'g'
 when freq = '40-49' then 'h'
 when freq = '50-59' then 'i'
 when freq = '60-69' then 'j'
 when freq = '70-79' then 'k'
 when freq = '80-89' then 'l'
 when freq = '90-99' then 'm'
 when freq = '100+' then 'n'
 else 'other' end as sort,
 freq,sum(imps) as imps,sum(uu) as uu from...
票数 1
EN

Stack Overflow用户

发布于 2018-03-18 04:32:54

假设freq列是数字(可能应该是),那么您的CASE表达式就会失败,因为3-6不是一个有效的数字。试一试这个版本:

代码语言:javascript
运行
复制
case
    when freq = 1 then 'a'
    when freq = 2 then 'b'
    when freq between 3 and 6 then 'c'
    when freq between 7 and 9 then 'd'
    when freq between 10 and 19 then 'e'
    when freq between 20 and 29 then 'f'
    ...
    when freq > 100 then 'n'
    else 'other' end as sort
票数 0
EN

Stack Overflow用户

发布于 2018-03-18 06:44:14

我认为这是你的问题:

代码语言:javascript
运行
复制
freq = '1' then 'a'

如果仔细查看引号,您会注意到它们与引用查询:)的类型相同:)这最终被计算为如下所示:

代码语言:javascript
运行
复制
freq = then

..。我觉得这不是你想要的。如果您使用双引号,它应该“只起作用”,减去查询中的任何语义错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49344466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档