简体   繁体   English

SQL计算一列中唯一值的数量并对该信息求和

[英]SQL counting numbers of unique values in a column and doing a sum of that info

I am trying to make a sum of the count of different values. 我试图对不同值的计数求和。

Here's an example : 这是一个例子:

 a a a b b b c c c d d d d e e e e 

The output would be : 输出将是:

5

Because there's 5 different values in that column. 因为该列中有5个不同的值。

Perform a Distinct Count which should give you the count of distinct values in a column and no need to do sum here 执行“非Distinct Count ,该操作应该为您提供一列中distinct值的count ,而无需在此处sum

select count(distinct colname) 
from yourtable

After searching and giving some good tought and testing here's the correct query : 搜索并给出一些好的建议并进行测试之后,下面是正确的查询:

SELECT SUM(uniqueValues) FROM (SELECT COUNT(DISTINCT values) as uniqueValues FROM tablename GROUP BY values) SELECT SUM(uniqueValues)FROM(SELECT COUNT(DISTINCT values)as uniquetables FROM tablename GROUP BY values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM