このクエリを合計関数で機能させることはできません
質問
このクエリはエラーを与えます:
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: "Type":無効な識別子
一緒に実行するとき 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