[英]SQL Distinct Count on Multiple Columns
I am new to SQL so don't know much about it. 我是SQL新手,所以对它了解不多。 Please help.
请帮忙。 I have a database something like this:
我有一个类似这样的数据库:
User Model
A X
A X
A X
B Y
C Y
C Y
D X
D X
E Z....
I want to calculate the frequency of the each unique model respective to unique users. 我想计算与唯一用户相对应的每个唯一模型的频率。 I mean the output should be something like this:
我的意思是输出应该是这样的:
Model Count
X 2
Y 2
Z 1
as A and D use model X, so X=2. 因为A和D使用模型X,所以X = 2。 Similarly B and C use model Y, so Y=2.
同样,B和C使用模型Y,因此Y = 2。 and same goes for Z(only user E).
Z(仅用户E)也是如此。 How do I achieve this?
我该如何实现?
Use group by
. 使用
group by
。
select model,count(distinct user)
from tbl
group by model
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.