简体   繁体   中英

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.

Perform a Distinct Count which should give you the count of distinct values in a column and no need to do sum here

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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