我试图使用bq
命令通过bash执行查询。通过web和app script @ script.google.com执行查询时都可以工作。但是,当作为一个简单的bash (或者仅仅通过cmd提示符)执行时,我一直在接收一个"Syntax error: Unexpected keyword THEN at [1:393]"
。Col 393指的是案件的中间部分陈述时。我已经做了大量的搜索,并且找不出我做错了什么(因为查询在其他地方工作)。可能有人会对导致错误的原因提出建议吗?
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
发布于 2018-03-18 08:35:00
这里的问题是由于在查询中使用相同的单引号造成的,这些单引号与您要包装整个查询的引号相同。
你的选择:
when freq = \'1\' then \'a\'
中的所有引号when freq = "1" then "a"
cat your_query.sql | bq query
选项3:是最优雅的解决方案,也有助于以一种很好的方式组织代码,将SQL保持为SQL,而不是将大量的SQL直接嵌入到bash代码中。您不需要对当前的SQL进行任何更改。
完整的示例如下所示
cat your_query.sql | bq query \
--destination_table abcdefcloud:ds_tables.daily_over_frequency_output \
--replace \
--use_legacy_sql=false
其中your_query.sql
将包含:
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...
发布于 2018-03-18 04:32:54
假设freq
列是数字(可能应该是),那么您的CASE
表达式就会失败,因为3-6
不是一个有效的数字。试一试这个版本:
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
发布于 2018-03-18 06:44:14
我认为这是你的问题:
freq = '1' then 'a'
如果仔细查看引号,您会注意到它们与引用查询:)的类型相同:)这最终被计算为如下所示:
freq = then
..。我觉得这不是你想要的。如果您使用双引号,它应该“只起作用”,减去查询中的任何语义错误。
https://stackoverflow.com/questions/49344466
复制相似问题