You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SQL透视表报错:Unrecognized name: Authorized问题排查求助

问题排查与修正方案

报错原因

你的SQL报错核心原因是PIVOT语法中枚举值的格式错误:
在BigQuery的PIVOT语法里,需要枚举的状态值(Authorized、Rejected)是字符串常量,必须用单引号包裹;而你用方括号[Authorized]会被数据库解析为「列名」,但当前上下文并没有名为Authorized的列,因此触发Unrecognized name错误。

另外你的子查询里还有一个语法小问题:select id_account, import, status_name as status,末尾多了一个逗号,这也会导致语法报错。

修正后的SQL代码

select id_account, `Authorized`, `Rejected`
from (
    select id_account, import, status_name as status
    from `mytable` t7
    where transaction_type_code in (6,9) 
      and authorization_date > '2022-06-01' 
      and authorization_date <= '2022-06-30'
) as src
pivot (
    sum(import) 
    for status in ('Authorized', 'Rejected')
) as pvt

关键说明

  1. PIVOT枚举值用单引号:将[Authorized], [Rejected]改为'Authorized', 'Rejected',明确告诉数据库这是status列的字符串取值。
  2. 查询列用反引号:因为Authorized和Rejected是PIVOT生成的列名,用反引号包裹可以避免与关键字冲突(属于SQL最佳实践)。
  3. 移除多余逗号:子查询中status_name as status后的逗号必须删除,否则会触发语法错误。

执行修正后的代码后,就能得到你预期的透视结果:按id_account分组,分别汇总Authorized和Rejected状态下的import总和。

内容的提问来源于stack exchange,提问作者Agustina Garcia Guevara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:24:30