avatar
I*9
1
2 variables in table1 showing all records of transactions each customer
makes. one customer (usedid 001) may purchase same products (productid Q909)
several times.
table1:
userid productid
001 Q909
001 Q909
001 Q908
002 Q101
Question: use SQL to calculate how many customers purchase only 1 type of
product, 2 types of products, 3...and so on.
for example: if there were 694 unique userid who purchased only 1 unique
productid, 900 unique userid purchased only 2 unique productid, we want to
get below table:
number_of_type_product_purchased number_of_customer
1 694
2 900
3 200

请高手指点吧
avatar
t*e
2
If the system doesn't support distinct count, do the following subquery
Select userid, count(productid) as ProdCount from
(select distinct userid, productid from Table)
group by userid

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
s*v
3
select userid, productid, count(*)
from (
select distinct * from table1
)
group by productid
好久不写sql了,就是先去除duplicate然后group by ?

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
d*e
4
select number_of_type_product_purchased, count(*) number_of_customer
from
(select userid, productid, count(*) number_of_type_product_purchased
from table1
group by userid, productid)
group by number_of_type_product_purchased
order by number_of_type_product_purchased, number_of_customer;

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
x*i
5
上面没一个可以运行的
avatar
d*e
6
could u post the error message here?
i don't have a machine to test right now.
Thanks.

【在 x*******i 的大作中提到】
: 上面没一个可以运行的
avatar
t*e
7
can you post your sql
Also we don't know the table name. you need insert the table name there.

【在 x*******i 的大作中提到】
: 上面没一个可以运行的
avatar
c*t
8
SELECT num_of_type_prod_purchased, COUNT (*) AS number_of_customer
FROM (SELECT user_id, COUNT (*) AS num_of_type_prod_purchased
FROM (SELECT DISTINCT user_id, product_id
FROM t1)
GROUP BY user_id)
GROUP BY num_of_type_prod_purchased;
运行测试通过,思路:先统计每个customer买了几种product, 再从结果中统计出买1种,2种。。。。各多少个customers

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
x*y
9
select cnt, count(usr_id)
from (select usr_id, count(distinct product_id) cnt
from table
group by usr_id
) newTable
group by cnt
order by cnt \g

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
c*e
10
sel prod_cnt, count(1) from (
sel userid, count(distinct productid) as prod_cnt
from table1 group by 1) a
group by 1
order by 1 ;

Q909)

【在 I*********9 的大作中提到】
: 2 variables in table1 showing all records of transactions each customer
: makes. one customer (usedid 001) may purchase same products (productid Q909)
: several times.
: table1:
: userid productid
: 001 Q909
: 001 Q909
: 001 Q908
: 002 Q101
: Question: use SQL to calculate how many customers purchase only 1 type of

avatar
i*e
11
select NoOfType as number_of_type_product_purchased, count(user) as number_
of_customer
from (
select userid, count(productid) as NoOfType
from (select distinct userid productid
from table1) a
group by userid) a1
group by NoOfType
order by number_of_type_product_purchased
avatar
j*s
12
Select number_of_type_product_purchased , count (*) as number_of_customer
from
(select UserID, count (distinct ProductID) as number_of_type_product_
purchased from table1
Group by UserID) as table2
Group by number_of_type_product_purchased
Order by number_of_type_product_purchased
Go
avatar
l*a
13
SELECT `cnt` AS number_of_type_product_purchased, COUNT(`userid`) AS number_
of_customer FROM
(
SELECT `userid`,COUNT(`productid`) AS cnt FROM `table1`
GROUP BY `userid`
) TMP
GROUP BY `cnt`
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。