Hello SQL Team!
Any idea why select count(distinct *)
does not work?
Thanks!
Hello SQL Team!
Any idea why select count(distinct *)
does not work?
Thanks!
Can you please define "does not work" ?
"(DISTINCT *)" has no meaning in SQL Server. COUNT ( * ) counts the presence of rows. To apply DISTINCT to the row, it must be outside parentheses.
Mmm, I want to count the distinct records of a table.
When I type:
select count(distinct *)
from table
It says:
Incorrect syntax near '*'.
WITH distinctRows
AS (
SELECT DISTINCT * FROM table;
)
SELECT COUNT(*) FROM distinctRows;
Or - use a derived table:
SELECT COUNT(*) FROM (SELECT DISTINCT * FROM table t)
Note: if you have a primary key defined - then all rows in the table are DISTINCT. If you don't have a primary key defined - why not?
Yes, because as I stated before, "(DISTINCT *)" has no valid meaning in SQL Server. It doesn't exist.