Search code examples
sqlsql-servert-sqlsql-server-2012sql-server-2014

SQL query to count number of rows with same value


I have this table data:

enter image description here

I want to perform an sql query which will give me the total number of distinct loan applications per city.

So for example, I would expect this output

City Wexford
Loans 1

City Waterford 1 
Loans 1

City Galway
Loans 3

Any idea what kind of query I need to perform to get the count of distinct loans for each city?


Solution

  • I would guess, probably a COUNT (Distinct ID) with GROUP BY. Something like this:

    SELECT city, COUNT(DISTINCT LoanApplicationID) as Loans
    FROM tableName
    GROUP BY city