BigQuery LEFT JOIN报错:INT64与STRING类型不匹配问题咨询
问题排查与解决
错误原因
报错提示No matching signature for operator = for argument types: INT64, STRING已经明确说明问题:两张表的login_id字段数据类型不兼容——一个是整数类型(INT64),另一个是字符串类型(STRING),BigQuery不允许不同类型的字段直接用=进行JOIN匹配,这就是第9行(JOIN的ON条件行)报错的核心原因。
解决方案
只需要把其中一个字段的类型转换成和另一个一致即可,以下是两种常用处理方式:
方式1:将字符串类型的login_id转为INT64(适用于字符串内容均为合法数字的场景)
如果确认字符串类型的login_id都是纯数字格式,直接用CAST转换即可:
SELECT performance.name, performance.ahtdn, tnps.tnps, FROM `data-exploration-2023.jan_scorecard_2023.performance-jan-2023` AS performance LEFT JOIN `data-exploration-2023.jan_scorecard_2023.tnps-jan-2023` AS tnps ON performance.login_id = CAST(tnps.login_id AS INT64)
方式2:将INT64类型的login_id转为STRING
如果不确定字符串类型的login_id是否全为数字,或者更倾向于统一用字符串类型匹配:
SELECT performance.name, performance.ahtdn, tnps.tnps, FROM `data-exploration-2023.jan_scorecard_2023.performance-jan-2023` AS performance LEFT JOIN `data-exploration-2023.jan_scorecard_2023.tnps-jan-2023` AS tnps ON CAST(performance.login_id AS STRING) = tnps.login_id
容错处理(可选)
如果字符串类型的login_id存在非数字内容,直接用CAST会触发报错,此时可以用SAFE_CAST——转换失败时返回NULL,不会中断整个查询:
SELECT performance.name, performance.ahtdn, tnps.tnps, FROM `data-exploration-2023.jan_scorecard_2023.performance-jan-2023` AS performance LEFT JOIN `data-exploration-2023.jan_scorecard_2023.tnps-jan-2023` AS tnps ON performance.login_id = SAFE_CAST(tnps.login_id AS INT64)
内容的提问来源于stack exchange,提问作者Mohamed Oraby
相关产品推荐
相关产品推荐

