我无法通过sum函数使此查询工作
题
此查询给出一个错误:
select ep,
case
when ob is null and b2b_ob is null then 'a'
when ob is not null or b2b_ob is not null then 'b'
else null
end as type,
sum(b2b_d + b2b_t - b2b_i) as sales
from table
where ...
group by ep, type
错误:ORA-00904:“类型”:无效标识符
当我与 group by ep
, ,错误消息变为:
ORA-00979:不是通过表达的组
如果我删除行,则整个查询正常 sum(b2b_d+b2b_t-b2b_i) as sales
和 group by ...
, ,因此问题应与按功能进行的总和和组有关。我该如何做这项工作?在此先感谢您的帮助。
解决方案
不幸的是,SQL不允许您按子句中的组中的列别名,因此您要么必须重复此类案例:
select ep,
case
when ob is null and b2b_ob is null then 'a'
when ob is not null or b2b_ob is not null then 'b'
else null
end as type,
sum(b2b_d + b2b_t - b2b_i) as sales
from table
where ...
group by ep,
case
when ob is null and b2b_ob is null then 'a'
when ob is not null or b2b_ob is not null then 'b'
else null
end
或使用这样的在线视图:
select ep,
type,
sum(b2b_d + b2b_t - b2b_i) as sales
from
( select ep,
case
when ob is null and b2b_ob is null then 'a'
when ob is not null or b2b_ob is not null then 'b'
else null
end as type,
b2b_d,
b2b_t,
b2b_i
from table
where ...
)
group by ep, type
不隶属于 StackOverflow