1

このクエリはエラーを返します:

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: GROUP BY式ではありません

sum(b2b_d+b2b_t-b2b_i) as sales行と行を削除するとgroup by ...、クエリ全体が正常に機能するため、問題は SUM および GROUP BY 関数に関連しているはずです。どうすればこれを機能させることができますか?よろしくお願いします。

4

1 に答える 1

4

残念ながら、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
于 2010-04-21T08:43:37.823 に答える