1

Oracle テーブルの 1 つの行で特定の値の出現回数を見つける必要があるような要件がありました。

ソーステーブルが次のようになっているとします。

ID   score1   score2   score3   score4 score5
---------------------------------------------
aa    -1       65       -1       -1      82
bb    -1       65       99       14      82

次のような結果を返すクエリが必要です。

ID   score1   score2   score3   score4 score5  count ( count of -1 occurences)
------------------------------------------------------
aa    -1       65       -1       -1      82     3
bb    -1       65       99       14      82     1

そして、私が使用しているオラクルのバージョンはOracle 10gです(したがって、PIVOTオプションの使用は除外されました)。誰でもこの問題の解決を手伝ってくれますか。

4

1 に答える 1

2

CASE または DECODE ステートメントのいずれかを使用できます。

select table_name.*,
       case score1 when -1 then 1 else 0 end +
       case score2 when -1 then 1 else 0 end +
       case score3 when -1 then 1 else 0 end +
       case score4 when -1 then 1 else 0 end +
       case score5 when -1 then 1 else 0 end
        as score_count
  from table_name;

サンプルはこちら

于 2013-09-18T04:18:30.877 に答える