のようになります。oracle where over()where句
別の特殊な質問です。私は、次のデータとクエリを持っている:
、それがどのように動作するかをwith helptab as (
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6762' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6763' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6835' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6835' as nr, 'B' as sub_val, 1 as sub_nr from dual
union all
select '6835' as nr, 'B' as sub_val, 1 as sub_nr from dual
union all
select '6835' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '6835' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'C' as sub_val, 1 as sub_nr from dual
union all
select '8904' as nr, 'C' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'C' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'C' as sub_val, 1 as sub_nr from dual
union all
select '8904' as nr, 'C' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8904' as nr, 'D' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'B' as sub_val, 1 as sub_nr from dual
union all
select '8905' as nr, 'B' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'C' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'A' as sub_val, 0 as sub_nr from dual
union all
select '8905' as nr, 'D' as sub_val, 0 as sub_nr from dual)
select nr, sub_val, sub_nr, count(sub_val) as cnt
, case when sub_val = 'A' then 0 when sum(count(sub_val)) over(partition by nr) >= 5 then count(sub_val) else 0 end as MoreThan5
, case when sub_val = 'A' then count(sub_val) when sum(count(sub_val)) over(partition by nr) < 5 then count(sub_val) else 0 end as LessThan5
from helptab
group by nr, sub_val, sub_nr
order by nr, sub_val, sub_nr
:
sub_valは「A」を等しいすべての行がLessThan5列になりましたしなければなりません。他はすべてnrとSub_valの合計でチェックする必要があります( 'A'を除く)。 5未満の場合は、すべての結果をLessThan5列に配置する必要があります。そうでない場合はMoreThan5に配置する必要があります。私はsum()over(Partition by)は正しい方法だと思ったが、正しく動作しなかった。
を問題の2つの単純なテーブル - サンプル・データ、所望の結果とは - 本当に他の人があなたが何をしたいのか理解するのに役立ちます。 –
私はちょうどあなたに(うまくいけば)それをより明確にするために写真を追加しました – user
質問を掲示するためのガイドラインを必ず読んでください。 http://stackoverflow.com/help/on-topic "デバッグのヘルプを求める質問(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、およびそれを再現するのに必要な最短コードが含まれていなければなりません明確な問題文がない質問は、他の読者にとっては有用ではありません。See:「最小限の完全かつ検証可能な例の作成方法」を参照してください。 –