为什么我在 BigQuery 中不断收到语法错误

Why do I keep getting a syntax error in BigQuery

提问人:Robert muriithi 提问时间:5/12/2023 最后编辑:halferRobert muriithi 更新时间:5/20/2023 访问量:145

问:

我似乎无法确定我做错了什么。这是我遇到的错误

运算符没有匹配的签名 = 对于参数类型:STRING, INT64 中。支持的签名:ANY = ANY at [15:8]

在此处输入图像描述

SELECT
  station_id,
  name,
  number_of_rides AS number_of_rides_starting_at_station
FROM 
  (
    SELECT 
      start_station_id,
      COUNT(*) number_of_rides
    FROM bigquery-public-data.new_york_citibike.citibike_trips
    GROUP BY 
      start_station_id
  ) subquery
  INNER JOIN bigquery-public-data.new_york_citibike.citibike_stations 
    ON station_id = subquery.start_station_id
ORDER BY
  number_of_rides DESC
sql mysql google-bigquery 语法错误 运算符

评论

2赞 jarlh 5/12/2023
这不是语法错误......您的表似乎具有不一致的数据类型。只有一个.哪些数据类型具有station_id和subquery.start_station_id?=
0赞 halfer 5/20/2023
在这里发帖之前,请花点时间学习 Markdown - 谢谢!

答:

0赞 Adrian Maxwell 5/12/2023 #1

消息“运算符没有匹配的签名 = 参数类型:STRING、INT64。支持的签名:ANY = ANY“表示 station_id 和 start_station_id 列具有不同的数据类型,一种是字符串,另一种是整数。

若要解决此问题,请将其中一列转换为另一列的数据类型。您需要弄清楚哪个是整数,哪个是字符串。例如

SELECT data_type
FROM `bigquery-public-data.new_york_citibike.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = 'citibike_stations' AND column_name = 'station_id'

我建议将整数列转换为字符串(所有整数都可以是字符串,但反之亦然)

SELECT
       station_id
     , name
     , number_of_rides AS number_of_rides_starting_at_station
FROM (
  SELECT start_station_id, COUNT(*) number_of_rides
  FROM bigquery-public-data.new_york_citibike.citibike_trips
  GROUP BY start_station_id
) subquery
INNER JOIN bigquery-public-data.new_york_citibike.citibike_stations
    ON CAST(station_id AS STRING) = subquery.start_station_id
ORDER BY number_of_rides DESC

当然,这只是一个猜测,也可能是相反的,即:

INNER JOIN bigquery-public-data.new_york_citibike.citibike_stations
    ON station_id = CAST(subquery.start_station_id AS STRING)

注意:您可以使用 safe_cast() 进行转换,但不需要它来将整数转换为字符串

评论

0赞 Robert muriithi 5/15/2023
谢谢!第二个奏效了。非常感谢