【问题标题】:SQL/snowflake: invalid identifierSQL/雪花:无效的标识符
【发布时间】:2021-08-04 05:48:31
【问题描述】:

试图了解为什么我的查询会收到错误“运行查询时出错:000904 (42000):SQL 编译错误:位置 48 处的错误行 16 无效标识符 'S1.V'”。

查询:

with step1 as (
select
    v:_id
from 
    PUBLIC.BUSINESSES 
where 
    v:created::date between DATEADD(dd, 1, last_day(DATEADD(mm, -3, GETDATE()))) and getdate()
group by 1
),

step2 as (
select
    distinct b.v:_id
from
    step1 s1
    inner join public.businesses b on b.v:_id = s1.v:_id
where 
    b.v:businessSetting = 'enabled'
)
    
select 'Businesses signedup' as step, count(*) as count from step1
union 
select 'Enabled' as step, count(*) as count from step2
order by count desc

有人可以帮忙吗?

【问题讨论】:

  • WITH step1 AS ( 在您的 SQL 开头丢失。
  • v:_id 和 v:created 是什么?
  • 对不起,我应该指定的!我的数据库被复制到 Snowflake 数据库中,该数据库为每个对象创建 json 变量 v

标签: sql snowflake-cloud-data-platform


【解决方案1】:

您没有从第 1 步返回 v,而是返回“V:_ID”。所以应该是:

with step1 as (
select
    v:_id
from 
    PUBLIC.BUSINESSES 
where 
    v:created::date between DATEADD(dd, 1, last_day(DATEADD(mm, -3, GETDATE()))) and getdate()
group by 1
),
step2 as (
select
    distinct b.v:_id
from
    step1 s1
    inner join public.businesses b on b.v:_id = s1."V:_ID"
where 
    b.v:businessSetting = 'enabled'
)
    
select 'Businesses signedup' as step, count(*) as count from step1
union 
select 'Enabled' as step, count(*) as count from step2
order by count desc;

我建议为该列定义一个别名并使用它:

with step1 as (
select
    v:_id AS ID
from 
    PUBLIC.BUSINESSES 
where 
    v:created::date between DATEADD(dd, 1, last_day(DATEADD(mm, -3, GETDATE()))) and getdate()
group by 1
),

step2 as (
select
    distinct b.v:_id
from
    step1 s1
    inner join public.businesses b on b.v:_id = s1.id
where 
    b.v:businessSetting = 'enabled'
)
select 'Businesses signedup' as step, count(*) as count from step1
union 
select 'Enabled' as step, count(*) as count from step2
order by count desc;

【讨论】:

  • 啊,是的,这是我犯的错误!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 2021-12-11
  • 2021-12-27
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多