Search code examples
sqlcountuniqueteradata

SQL: How do you count the number of unique records?


I'm new to SQL and sorry if this q has been asked before - I couldn't phrase it properly.

Say I have a table that looks like this:

Name    Call ID
Sally   1
Sally   2
Sally   3
Mike    4
Mike    5
Bob     6
Bob     7

I want to create a new table that looks like this:

Name    No. of calls
Sally   3
Mike    2
Bob     2

Attempt

I assume I would do something like:

SELECT 
Name, 
COUNT(distinct Name) AS No. of Calls 
FROM Table

Thanks.


Solution

  • You just need to group them and that's all.

    SELECT
         Name
         COUNT(*) AS [No. of Calls]
    FROM
         Table
    GROUP BY
         Name