使用查询中定义的别名时找不到列

Column not found when using an alias defined in the query

提问人:Thales Campolim 提问时间:10/28/2023 最后编辑:marc_sThales Campolim 更新时间:10/28/2023 访问量:53

问:

我有一个查询,我用它来收集一个 id,它可以根据用户的电话号码存在不同的表。问题是,当我从表上的列中提取电话号码后,尝试使用我定义的别名加入其他表时,我发现找不到别名列。

查询如下:

select
    substring(regexp_extract(t.to, '(\d+)@*', 1), -11, 11) as phone,
    case
        when u.id is not null 
            then cast(u.id as varchar)
        when a.codigo_do_usuario__c is not null 
            then a.codigo_do_usuario__c
        when ac.codigo_do_usuario__c is not null 
            then ac.codigo_do_usuario__c
        when l.codigo_do_usuario__c is not null 
            then l.codigo_do_usuario__c
        when ld.codigo_do_usuario__c is not null 
            then ld.codigo_do_usuario__c
        else ''
    end as id
from
    template as t
left join 
    users u on substring(t.phone, -11, 11) = substring(u.phone, -11, 11)
left join 
    account a on substring(t.phone, -11, 11) = substring(a.personmobilephone, -11, 11)
left join 
    account ac on substring(t.phone, -11, 11) = substring(ac.phone, -11, 11)
left join 
    lead l on substring(t.phone, -11, 11) = substring(l.mobilephone, -11, 11)
left join 
    lead ld on substring(t.phone, -11, 11) = substring(ld.phone, -11, 11)
group by 
    1,2

我收到错误

AWS Athena 客户端引发了错误。COLUMN_NOT_FOUND:第 21:57 行:无法解析列“t.phone”或请求者无权访问请求的资源 [执行 ID:90235f99-1d02-439b-b091-08b81bac4f0a]

使用子字符串确保我只获得列中的最后 11 个数字,以避免国家/地区代码。

我想从电话号码中获取可以在这些表中的任何一个中的 ID

SQL 正则表达式 联接 别名 Amazon-Athena

评论

0赞 Thorsten Kettner 10/28/2023
t.phone?表中是否有列(在查询中调用)?似乎不是这样。phonetemplatet
3赞 Stu 10/28/2023
可能是最常被问到的问题之一。例如,请参阅 using-alias-in-query-and-using-it why-cant-i-use-column-aliases-in-the-next-select-expression how-to-use-alias-column-name-in-where-clause-in-sql-server
0赞 jarlh 10/28/2023
今日小贴士:!例如。COALESCE()coalesce(cast(u.id as varchar), a.codigo_do_usuario__c, ...)

答: 暂无答案