转置和 Where 标准

Transposing and Where Criteria

我想知道是否有更有效的编写下面的代码(不起作用)。我正在尝试转置数据集,但只需要 Dogs 和 Cats 大于 1 的值。因为在一个组中同时有猫和狗。

下面的代码不起作用,但是我尝试基于上面的语句进行拉取。请随意修改下面的代码and/or提供更好的代码。提前致谢!

    select 
    ,group_id
    ,sum(case when product = '18' then 1 else 0 end) as "Dogs"
    ,sum(case when product = '20' then 1 else 0 end) as "Cats"

    from risk_bl

    where dogs > 1 and cats > 1

    group by 1

尝试使用 HAVING 子句:

select 
,group_id
,sum(case when product = '18' then 1 else 0 end) as "Dogs"
,sum(case when product = '20' then 1 else 0 end) as "Cats"

from risk_bl
group by 1
having sum(case when product = '18' then 1 else 0 end) > 1 
and sum(case when product = '20' then 1 else 0 end)> 1

正如 dnoeth 指出的那样,Teradata 允许您在 having 子句中使用别名(除其他外),因此您的 having 子句也可以是:

 having dogs > 1 and cats > 1

having子句在分组和聚合之后应用,我想这就是你想要的。