0

Oracleベースのシステムを使用しています。

like、having、caseステートメントを一緒にどのように使用しますか?

私は基本的に、4つを超える「クラスA」トランザクションまたは1つを超える「クラスB」トランザクションを持つトランザクションテーブルで見つかったすべての一意の個人をリストしようとしています。私が使用したい理由likeは、トランザクションクラスを区別する唯一の方法はlike、トランザクションタイプ列のステートメントを使用することであるためです。

たとえば、多くのトランザクションタイプがありますが、トランザクションタイプの一部として含まれているのは「クラスA」のみであり、「クラスB」は、トランザクションタイプ列に'%ABC%'ない他のすべてのタイプです。'%ABC%'

繰り返しになりますが、クエリで4つを超える「クラスA」トランザクションまたは1つの「クラスB」トランザクションを持つ個人IDのみを返すようにします。

これが私がこれまでに持っているものです:

select tt.indiv_id, count(*) from transactiontable tt
group by tt.indiv_id
case when tt.tran_type like '%ABC'
having count(*) > 4
else
having count(*)>1.

私はサイトでかなり検索しましたが、これらの機能をすべて一緒に使用した例は見つかりませんでした。

4

3 に答える 3

1
select tt.indiv_id, 
    count(case when tt.tran_type like '%ABC' then 1 end) as ClassACount,
    count(case when tt.tran_type not like '%ABC' then 1 end) as ClassBCount
from transactiontable tt
group by tt.indiv_id
having count(case when tt.tran_type like '%ABC' then 1 end) > 4
    or count(case when tt.tran_type not like '%ABC' then 1 end) > 1
于 2012-09-05T20:21:11.610 に答える
0

これを試して

select tt.indiv_id, count(*) 
from transactiontable tt  
group by tt.indiv_id, tt.tran_type
having count(*) > case when tt.tran_type like '%ABC' then 4 else 1 end
于 2012-09-05T20:19:24.010 に答える
0

クエリは終了しました。持っている句で各トランザクションタイプを個別に追跡する必要があります。

select tt.indiv_id, count(*)
from transactiontable tt
group by tt.indiv_id
having sum(case when tt.tran_type like '%ABC%' then 1 else 0 end) > 4 or
       sum(case when tt.tran_type not like '%ABC%' then 1 else 0 end) > 1
于 2012-09-05T20:21:39.050 に答える