I can select all the distinct values in a column in the following ways:
SELECT DISTINCT column_name FROM table_name;SELECT column_name FROM table_name GROUP BY column_name;
But how do I get the row count from that query? Is a subquery required?
I can select all the distinct values in a column in the following ways:
SELECT DISTINCT column_name FROM table_name;SELECT column_name FROM table_name GROUP BY column_name;But how do I get the row count from that query? Is a subquery required?
You can use the DISTINCT keyword within the COUNT aggregate function:
SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name
This will count only the distinct values for that column.
select A,COUNT(DISTINCT B) from table group by ABe aware that Count() ignores null values, so if you need to allow for null as its own distinct value you can do something tricky like:
select count(distinct my_col)
+ count(distinct Case when my_col is null then 1 else null end)
from my_table
/
case when my_col is null then 1 else my_col endSELECT my_col, COUNT(my_col) + COUNT(CASE WHEN my_col IS NULL THEN 1 ELSE NULL END) as CountOf from my_Table GROUP BY my_colcount(*) is specifically a count of rows.To do this in Presto using OVER:
SELECT DISTINCT my_col,
count(*) OVER (PARTITION BY my_col
ORDER BY my_col) AS num_rows
FROM my_tbl
Using this OVER based approach is of course optional. In the above SQL, I found specifying DISTINCT and ORDER BY to be necessary.
Caution: As per the docs, using GROUP BY may be more efficient.
Count(distinct({fieldname})) is redundant
Simply Count({fieldname}) gives you all the distinct values in that table. It will not (as many presume) just give you the Count of the table [i.e. NOT the same as Count(*) from table]
count(field) returns the number of lines where field is not null.