您好,这是我的数据样本
coustmer_NO id
1 5
1 13
2 4
2 4
2 4
3 4
3 10
4 8
4 8
使用SQL>>我想为每个客户计算他们有多少不同的ID。 预期的产出是:
coustmer_NO total_id
1 2
2 1
3 2
4 1
答案 0 :(得分:1)
我猜您的数据中存在拼写错误
结果应为:
coustmer_NO total_id
1 2
2 1
3 2
4 1
您可以执行以下操作:
SELECT costumer_NO, count(distinct id) AS total_id FROM <table_name> GROUP BY costumer_NO;
答案 1 :(得分:0)
在MYSQL中尝试此查询:
select coustmer_NO, count(distinct id) as 'total_id' from table_name group by coustmer_NO;