2016-04-19 30 views
-1

enter image description here
のようになります。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)は正しい方法だと思ったが、正しく動作しなかった。

+4

を問題の2つの単純なテーブル - サンプル・データ、所望の結果とは - 本当に他の人があなたが何をしたいのか理解するのに役立ちます。 –

+0

私はちょうどあなたに(うまくいけば)それをより明確にするために写真を追加しました – user

+0

質問を掲示するためのガイドラインを必ず読んでください。 http://stackoverflow.com/help/on-topic "デバッグのヘルプを求める質問(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、およびそれを再現するのに必要な最短コードが含まれていなければなりません明確な問題文がない質問は、他の読者にとっては有用ではありません。See:「最小限の完全かつ検証可能な例の作成方法」を参照してください。 –

答えて

1

私はすべてを正しく理解している場合、二つの小さな変更が必要とされています

select nr, sub_val, sub_nr, count(sub_val) as cnt, 
     case 
     when sub_val = 'A' then 0 
     when sum(count(case when sub_val <> 'A' then sub_val end)) -- <-- here 
       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(case when sub_val <> 'A' then sub_val end)) -- <-- and here 
       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 
+0

完璧!それはまさに私が必要とするものです。どうもありがとう。 – user

+0

あなたは大歓迎です。私は 'sum(count(...))'を単純な 'count(...) 'に変更できると思っていますが、それを確認してください。 –

+0

このクエリが有用なことをするのに驚いています。条件付き集計の条件として 'group by'列を使用することは珍しいようです。 –