0

次の列を持つテーブルがあるとします。

顧客ID、製品ID

私が見たいのは、100,000 人の顧客がいる場合、それらをグループ化して、X 人の顧客が Y 個の製品を持っていることを確認できるようにすることです。たとえば、2,000 人の顧客には 5 つの購入があり、1,000 人の顧客には 4 つの製品があり、10 人の顧客には 25 の製品がある場合があります。X 個の製品を持つ顧客の数に基づいてグループ化するにはどうすればよいですか?

データベースはオラクル。

サンプル データ セット:

customerID  productId
---------- ----------
 12345      33035
 12345      33049
 12345      33054
 56789      32777
 56789      32897
 56789      32928
 56789      32958
 56789      33174
 56789      33175
 56789      33410
 56789      35101
 67890      32777
 67890      32897
 67890      32928
 67890      32958
 67890      33174
 67890      33175
 67890      33410
 67890      35101
  45678      33035
  45678      33289
  45678      34354
  45678      36094
 23456      32778
 23456      33047
 23456      33051
 34567      32776
 34567      32778
 34567      33162

これにより、3 つの製品を持つ 3 人の顧客、8 つの製品を持つ 2 つの顧客、および 4 つの製品を持つ 1 つの顧客が存在する、このグループ化 (サンプル データ セットに基づく) が得られます。

number_customers    number_products
3           3
2           8
1           4

一連の group by ステートメントを試しましたが、何か不足しています。どんな助けでも大歓迎です。

ありがとう

4

3 に答える 3

2
SELECT
  COUNT(CustomerID) AS number_customers,
  number_products
FROM (
  SELECT CustomerID, 
  COUNT(ProductID) AS number_products
  FROM tableName
  GROUP BY customerID
) subquery
GROUP BY number_products
于 2013-07-10T21:28:00.297 に答える
0
Select CustomerID, count(ProductID)
FROM tableName
group by customerID
having count(ProductID) > 25

個別の製品のみをカウントする場合...

   Select CustomerID, count(distinct ProductID)
    FROM tableName
    group by customerID
    having count(ProductID) > 25

データには、製品ごとに毎回リストされている顧客 1 が含まれており、複数の顧客が同じ製品に関連付けられている可能性があると仮定します。

于 2013-07-10T21:16:14.527 に答える
0
create table cust_pur as select floor(dbms_random.value(1,21)) customer_id,         floor(dbms_random.value(1,21)) product_id from dual connect by level <= 100 order by customer_id

select * from cust_pur order by customer_id


with k1 as (
select distinct(customer_id), count(product_id)  over ( partition by customer_id ) cnt from cust_pur order by customer_id
)
select count(customer_id),cnt from k1 group by cnt
于 2013-07-10T23:45:16.820 に答える