简体   繁体   English

BigQuery 意外关键字

[英]BigQuery unexpected keyword

I'm just getting started with Google BigQuery, and have run into issues with my very first query.我刚刚开始使用 Google BigQuery,并且在我的第一个查询中遇到了问题。 I'm trying to get a list of Stack Overflow posts since and including 2015-01-01 which have one of several tags.我正在尝试获取自 2015 年 1 月 1 日以来(包括 2015 年 1 月 1 日)的 Stack Overflow 帖子列表,这些帖子具有多个标签之一。 Below is my first pass at the query:以下是我在查询中的第一遍:

#standardSQL

SELECT
 title,
 body,
 answer_count,
 creation_date,
 tags,
 view_count 

FROM
 `bigquery-public-data.stackoverflow.posts_questions` limit 10

WHERE
 creation_date >= "2015-01-01" AND tags HAVING "terraform" OR "chef" OR "puppet" OR "ansible"

The BigQuery validator is showing the following error message: BigQuery 验证器显示以下错误消息:

Error : Syntax error: Unexpected keyword WHERE at [14:1]错误:语法错误:[14:1] 处的意外关键字 WHERE

You have a few syntax errors, namely the limit 10 in the wrong place, and using the HAVING keyword incorrectly. 您有一些语法错误,即错误位置的limit 10和错误使用HAVING关键字。 I'd also use native timestamp instead of comparing strings: 我还将使用本机timestamp而不是比较字符串:

#standardSQL
SELECT
  title,
  body,
  answer_count,
  creation_date,
  tags,
  view_count
FROM
  `bigquery-public-data.stackoverflow.posts_questions`
WHERE
  creation_date >= TIMESTAMP('2015-01-01')
  AND tags IN ('terraform',
    'chef',
    'puppet',
    'ansible')
LIMIT
  10

There are a few issues here, but hopefully this will help: 这里有一些问题,但是希望这会有所帮助:

  • LIMIT needs to come last, after the WHERE clause. LIMIT需要排在WHERE子句之后。
  • While HAVING is valid in some contexts--namely as a post-aggregation (ie after GROUP BY) filter, it doesn't have the meaning that I think you want here. 虽然HAVING在某些情况下有效-即作为后聚合(即GROUP BY之后)过滤器,但它在这里没有我想要的含义。

With that said this query might be what you want: 这样说来,此查询可能就是您想要的:

#standardSQL
SELECT
  title,
  body,
  answer_count,
  creation_date,
  tags,
  view_count
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE creation_date >= "2015-01-01" AND
  EXISTS (
    SELECT 1 FROM UNNEST(SPLIT(tags, "|")) AS tag
    WHERE tag IN ("terraform", "chef", "puppet", "ansible")
  )
LIMIT 10;

Note that I needed to use SPLIT with the tags column because the tags are separated by the pipe character. 请注意,我需要在tags列中使用SPLIT ,因为标签由竖线字符分隔。 Since you get a terabyte of querying for free, try to make the most of it by getting all the results at once rather than using the LIMIT, too. 由于您可以免费获得TB级的查询,因此请尝试一次获取所有结果而不是使用LIMIT来充分利用它。

SELECT SELECT
usertype, CONCAT(start_station_name, " to ",end_station_name) AS route, COUNT(*) as num_trips, ROUND(AVG(cast(tripduration as int64)/60),2) AS duration FROM bigquery-public-data.new_york_citibike.citibike_trips GROUP BY start_station_name, end_station_name,usertype, ORDER BY num_trips DESC LIMIT 10用户类型,CONCAT(start_station_name, " to ",end_station_name) AS route, COUNT(*) as num_trips, ROUND(AVG(cast(tripduration as int64)/60),2) AS duration FROM bigquery-public-data.new_york_citibike.citibike_trips GROUP BY start_station_name,end_station_name,usertype,ORDER BY num_trips DESC LIMIT 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BigQuery 查找访问过 pageA(包含关键字“main”)和 pageB(包含关键字“side”)的会话 - BigQuery finding sessions that have visited both pageA (contains keyword "main") and pageB (contains keyword "side") 尝试在 BigQuery 上创建表时收到“意外错误”消息 - Getting "unexpected error" message when trying to create table on BigQuery BigQuery SELECT NULL AS“some_name”:意外的字符串文字 - BigQuery SELECT NULL AS "some_name" : Unexpected String Literal 在 BigQuery 中加载 avro 文件 - 默认值的意外类型。 预期为 null,但找到了字符串:“null” - Load a avro file in BigQuery - Unexpected type for default value. Expected null, but found string: "null" 在 BigQuery 中读取时,未定义行 XXXX、列 xx-xx 处的 JSON 输入意外结束 - Unexpected end of JSON input at undefined line XXXX, columns xx-xx while reading in BigQuery 无法将具有 JSON/RECORD 列类型的 bigquery 表读入 spark dataframe。(java.lang.IllegalStateException:意外类型:JSON) - Unable to read bigquery table with JSON/RECORD column type into spark dataframe. ( java.lang.IllegalStateException: Unexpected type: JSON) client.get_bucket() 返回错误:api_request() 得到了一个意外的关键字参数“extra_api_info” - client.get_bucket() returns error: api_request() got an unexpected keyword argument 'extra_api_info' Bigquery 中的 For 循环 - For loop in Bigquery 当前在 BigQuery 中? - CURRENT in BigQuery? Bigquery 到 PubSub - Bigquery to PubSub
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM