Trying to find code that will allow me to count first names by group, and then return the name with the lowest count.
Sample data:-
PersonGroup FirstName
------------------------
A Bob
A Mary
A Bob
A Bob
B Michelle
B Michelle
B Greg
B Greg
B Michelle
C Cindy
C Michelle
C Michelle
D Rod
D Rod
D Rod
D Rod
D Rod
D Mary
D Mary
D Mary
D Mary
D Mary
D Mary
Output required :
PersonGroup FirstName Count
--------------------------------
A Mary 1
B Greg 2
C Cindy 1
D Rod 5
First Name columns has the name that occurs the least within the group
Count column has the count of Name that occurs the least amount of times per group
this is my code so far, but every name is being returned,
select
PersonType,
FirstName,
count (firstName) as mycount
from
[Person].[Person]
group by
FirstName,
[PersonType]
having
count(firstName) = (select min(a.cnt)
from
(select count(firstname) as cnt
from [Person].[Person]
group by [FirstName]) as a)
order by
PersonType desc