2017-09-26 10 views
0

トランザクションテーブルでは、order_ID(PK)、transaction_date、cus_ID、store_IDがあります。 cus_ID、count(store_ID)= 1でグループ化するときにすべてのcus_IDを取得する必要があります.1つの例外はありますが - cus_IDにstore_ID = '999'の注文のみがある場合。 取引日は過去365日間である必要があります。 私は以下のようにコードを持っている:group byとcount()を使用するときの特別な条件の除外方法

Select cus_ID, count(store_ID) as store_count 
From trn_header 
where transanction_date From '7/30/2017' - 365 To '7/30/2017' 
group by cus_ID having store_count = 1 

私はちょうど 感謝

+0

'store_ID = '999''のときは何ですか?あなたは 'CASE'ステートメントが必要ですが、私はあなたのロジックを理解していません。 – Simon

+0

1つのcust_idがstore_ID = '999'というようにすべてのオーダーを持っている場合、それは私のクエリによって除外されるべきです。 –

答えて

0

ことことを願って... '999' の条件を除外する方法がわかりません正しい回答は次のようにする必要があります:

select distinct cus_id, store_id from 
(select cus_id,count(distinct store_id) as freq 
from a 
where transaction_date between @begin_date and @end_date 
group by cus_id 
having count(distinct store_id) = 1 
) b, a where a.cus_id = b.cus_id and store_id <> '999' 
0
Select cus_ID, count(store_ID) as store_count 
From trn_header 
where transanction_date From '7/30/2017' - 365 To '7/30/2017' And store_ID<>'999' 
group by cus_ID having store_count = 1 

が、それは参考になりまし

+0

store_id = '999'の場合を除外するためにサブクエリを使用する必要があります。私の質問から誤解があります。申し訳ありません。 –

関連する問題