К сожалению, SQL не позволяет вам использовать псевдонимы столбцов в предложении GROUP BY, поэтому вы должны либо повторить весь CASE там следующим образом:
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