Here's the SQL that works (strangely) but still just returns the COUNT of all items, not the COUNT of DISTINCT items in the column.
SELECT DISTINCT(COUNT(columnName)) FROM tableName;
Counting Unique Values in Excel. Unique value in excel appears in a list of items only once and the formula for counting unique values in Excel is “=SUM(IF(COUNTIF(range,range)=1,1,0))”. The purpose of counting unique and distinct values is to separate them from the duplicates of a list of Excel.
Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table.
SELECT COUNT(*) FROM tableName
counts all rows in the table,
SELECT COUNT(columnName) FROM tableName
counts all the rows in the table where columnName is not null, and
SELECT (DISTINCT COUNT(columnName)) FROM tableName
counts all the rows in the table where columnName is both not null and distinct (i.e. no two the same)
SELECT DISTINCT(COUNT(columnName)) FROM tableName
Is the second query (returning, say, 42), and the distinct gets applied after the rows are counted.
You need
SELECT COUNT(DISTINCT columnName) AS Cnt
FROM tableName;
The query in your question gets the COUNT
(i.e. a result set with one row) then applies Distinct
to that single row result which obviously has no effect.
SELECT COUNT(*) FROM (SELECT DISTINCT columnName FROM tableName);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With