Count(distinct case when)
- distinct count query in sql
- count distinct statement in sql
- is count distinct in sql
- distinct count sql server
Count distinct excel.
Sql distinct count group by
Problem
You’d like to count how many different non-NULL values there are in a given column.
Example
Our database has a table named with data in the following columns: , , , and .
| id | first_name | last_name | city |
|---|---|---|---|
| 1 | John | Williams | Chicago |
| 2 | Tom | Brown | Austin |
| 3 | Lucy | Miller | Chicago |
| 4 | Ellie | Smith | Dallas |
| 5 | Brian | Jones | Austin |
| 6 | Allan | Davis | NULL |
Let’s find the number of different (and non-) cities.
Solution
SELECT COUNT(DISTINCT city) as cities FROM customer;This query returns number of cities where customers live:
Discussion
To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the function as .
When given a column, returns the number of values in that column. Combining this with returns only the number of unique (and non-NULL) values.
Recommended courses:
Recommended articles:
See also:
- sql count distinct count