0

Customer_ID に複数の Prospect_ID が関連付けられている行を返すクエリを作成する必要があります。たとえば、Customer_ID は同じですが、Prospect_ID は異なりますが、Prospect_id が同じであるため、行 5 と 6 は異なるため、クエリで次の行 2 と 3 を返すようにします。

Prospect_ID   Customer_ID
1001          31001
1002          31002
1003          31002
1004          31003
1005          31004
1005          31004 
4

2 に答える 2

4

Customer_id複数の distinct を使用してを取得するにはProspect_id:

select customer_id
from yourtable
group by customer_id
having count(distinct prospect_id) >1

デモで SQL Fiddle を参照してください

それらのすべての詳細を返したい場合は、Customer_Ids次を使用できます。

select *
from yourtable t1
where exists (select customer_id
              from yourtable t2
              where t1.customer_id = t2.customer_id
              group by customer_id
              having count(distinct prospect_id) >1)

SQL Fiddle with Demoを参照してください。

これは次のように書くこともできます (thanks @ypercube ):

select *
from yourtable t1
where exists (select customer_id
              from yourtable t2
              where t1.customer_id = t2.customer_id
              and t1.prospect_id <> t2.prospect_id)

デモで SQL Fiddle を参照してください

于 2013-02-12T20:17:21.977 に答える
0

ウィンドウ関数でこれを行うことができます:

select prospect_id, customer_id
from (select t.*,
             COUNT(*) over (partition by prospect_id, customer_id) as cnt_pc,
             COUNT(*) over (partition by customer_id) as cnt_c
      from t
     ) t
where cnt_pc <> cnt_c and cnt_c > 1
于 2013-02-12T20:26:54.693 に答える