SQL编译错误:[TABLE.CRM_ID]不是一个有效的GROUP BY表达式

huangapple go评论69阅读模式
英文:

SQL compilation error: [TABLE.CRM_ID] is not a valid group by expression

问题

运行我的代码时,出现了标题中的错误。我正在尝试获取客户在其最早订阅日期时拥有的第一个产品集。我正在dbt中运行这个代码。

以下是引入表格的代码:

, products_extended as (
    select
    crm_id,
    term_start_date,
    rate_plan_name,
    product_category
    from {{ref('products_extended')}}
)

然后,尝试按每个帐户的最早订阅日期分组,并使用array_agg聚合产品/类别名称。

, first_product_set as (
    select
    crm_id,
    term_start_date,
    array_agg(distinct rate_plan_name) as first_rate_plan_names,
    array_agg(distinct product_category) as first_rate_plan_categories
    from products_extended
    where rate_plan_name not like '%Free Trial%' and rate_plan_name not like '%Free Access%'
    qualify row_number() over(partition by crm_id, term_start_date order by term_start_date asc) = 1
)

我只想保留每个帐户基于term_start_date的第一行。说crm_id不是有效的分组依据。有什么建议吗?

代码如上所示。我看到一些嵌套聚合的示例存在这些问题,但那并没有起作用。不确定接下来该怎么做。

英文:

Getting the error in the title when I run my code. I am trying to get the first product sets a customer had at their earliest subscription date. I am running this in dbt.

Here's the code bringing in the table:

, products_extended as (
    select
    crm_id,
    term_start_date,
    rate_plan_name,
    product_category
    from {{ref('products_extended')}}
)

Then attempting to group by earliest subscription for each account and array_agg the product/category names.

, first_product_set as (
    select
    crm_id,
    term_start_date,
    array_agg(distinct rate_plan_name) as first_rate_plan_names,
    array_agg(distinct product_category) as first_rate_plan_categories
    from products_extended
    where rate_plan_name not like '%Free Trial%' and rate_plan_name not like '%Free Access%'
    qualify row_number() over(partition by crm_id, term_start_date order by term_start_date asc) = 1
)

I only want to keep the first row per account based on term_start_date. Saying crm_id is not a valid group by. Any suggestions?

Code is shown above. I saw some examples nesting aggregations with problems like these, but that didn't work. Not sure where to go from here

答案1

得分: 1

, products_extended 作为扩展产品表

select
crm_id,
term_start_date,
rate_plan_name,
product_category,
row_number() over(partition by crm_id, term_start_date order by term_start_date asc) rn
from {{ref('products_extended')}}

然后进行聚合操作

, first_product_set 作为第一个产品集合表

select
crm_id,
MIN(term_start_date),
array_agg(distinct rate_plan_name) as first_rate_plan_names,
array_agg(distinct product_category) as first_rate_plan_categories
from products_extended
where rate_plan_name not like '%Free Trial%' and rate_plan_name not like '%Free Access%'
and rn = 1
GROUP BY crm_id
英文:

The row_number you can add in the first query

, products_extended as (
    select
    crm_id,
    term_start_date,
    rate_plan_name,
    product_category,
    row_number() over(partition by crm_id, term_start_date order by term_start_date asc) rn
    from {{ref('products_extended')}}
)

and then aggregate

, first_product_set as (
    select
    crm_id,
    MIN(term_start_date),
    array_agg(distinct rate_plan_name) as first_rate_plan_names,
    array_agg(distinct product_category) as first_rate_plan_categories
    from products_extended
    where rate_plan_name not like '%Free Trial%' and rate_plan_name not like '%Free Access%'
    and rn = 1
GROUP BY crm_id
)

huangapple
  • 本文由 发表于 2023年7月7日 05:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定