使用组合使用min()、count()、having子句的聚合的sql

使用组合使用min()、count()、having子句的聚合的sql,sql,count,min,having,Sql,Count,Min,Having,试图找到允许我按组数名的代码,然后返回计数最低的名称 样本数据:- PersonGroup FirstName ------------------------ A Bob A Mary A Bob A Bob B Michelle B Michelle B Greg B Greg B

试图找到允许我按组数名的代码,然后返回计数最低的名称

样本数据:-

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
所需输出:

PersonGroup   FirstName    Count
--------------------------------
    A         Mary           1
    B         Greg           2
    C         Cindy          1
    D         Rod            5
“名字”列的名称在组中出现最少

Count列的名称计数在每个组中出现的次数最少

这是我到目前为止的代码,但每个名字都会被返回

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
您可以使用row_number()


使用窗口功能
行号()


在第二种方法中,对所有行使用行号,行号将为1。您需要从over子句中删除firstname分区,然后它将正常工作。不要使用第一个分区,它会多次解析整个表。没有必要。第一个答案是个糟糕的答案。然而,第二个答案是完美的:)领带呢?例如,如果一个组的两个(或多个)名称只出现一次,您希望返回什么?对于SQL问题,您应该始终标记您的DBMS。非标准括号看起来像SQL Server。这就是您正在使用的DBMS吗?样本数据很差。删除(B,Michelle)行中的一行,使其更复杂一点!(并相应调整预期结果。)
select a.*
from (select PersonType,FirstName ,count (firstName) as mycount, 
             row_number() over (partition by PersonType order by count(*)) as rn
      from [Person].[Person]
     group by FirstName,[PersonType]
     ) a
where rn= 1; 
    with cte as
(
  select 'A' as PersonGroup, 'Bob' as name
  union all
  select 'A', 'Mary'
   union all
  select 'A', 'Mary'
    union all
  select 'B', 'Michelle'  
    union all
  select 'B', 'Greg'  
     union all
  select 'B', 'Greg' 
    union all
  select 'B', 'Michelle'
     union all
  select 'B', 'Michelle'
    union all
  select 'C', 'Michelle'
     union all
  select 'C', 'Michelle'
     union all
  select 'C', 'Cindy'
     union all
  select 'D', 'Rod'  
     union all
  select 'D', 'Rod'  
     union all
  select 'D', 'Rod'
    union all
  select 'D', 'Rod'  
     union all
  select 'D', 'Rod'
     union all
  select 'D', 'Mary'
     union all
  select 'D', 'Mary'
     union all
  select 'D', 'Mary'
     union all
  select 'D', 'Mary'
     union all
  select 'D', 'Mary'
     union all
  select 'D', 'Mary'
)
 , cte3 as (
  select personGroup, name, COUNT(*) as cnt, row_number() over(partition by PersonGroup order by COUNT(*) ) rn  from cte GROUP BY personGroup, name
      ) select PersonGroup,name,cnt from cte3 where rn=1