如何查找使用SQL中的函数计算计数的组的最大计数?

时间:2019-01-30 04:51:49

标签: sql sql-server group-by

我有一个由customer_id和keyword_id组成的表。 customer_id和keyword_id的不同组合多次出现,我想为每个customer_id查找出现率最高的keyword_id。我该怎么办?

   Customer_ID . Keyword_ID
      1                 a
      1                 a
      1                 a
      1                 b
      1                 b
      2                 c
      2                 c
      2                 c
      2                 d

预期结果

Customer_ID .  Max_Keyword_ID . Count
1                 a                 3
2                 c                 3

4 个答案:

答案 0 :(得分:1)

您可以利用count和density_rank获得预期的输出。获取等级= 1,以确保获得的行中出现给定输出的次数最多。

with cte as (
  select  1 as customer_id, 'a' as Keyword_ID  union all 
 select  1 as customer_id, 'a' as Keyword_ID union all  
 select  1 as customer_id, 'a' as Keyword_ID union all  
 select  1 as customer_id, 'b' as Keyword_ID union all  
 select  1 as customer_id, 'b' as Keyword_ID union all  
 select  2 as customer_id, 'c' as Keyword_ID union all  
 select  2 as customer_id, 'c' as Keyword_ID union all  
 select  2 as customer_id, 'c' as Keyword_ID union all  
 select  2 as customer_id, 'd' as Keyword_ID) 

 SELECT customer_id, Keyword_ID, [COUNT]  FROM (
 select customer_id, Keyword_ID, count(1) [COUNT],
 dENSE_RANK() OVER (PARTITION BY customer_id ORDER BY COUNT(1) DESC) RANKED  from cte C 
 group by customer_id, Keyword_ID ) Z 
 WHERE Z.RANKED = 1 

输出:

   customer_id  Keyword_ID  COUNT
      1            a         3
      2            c         3

答案 1 :(得分:0)

您可以在下面尝试-使用相关子查询

with cte as
(
select Customer_ID,Keyword_ID,count(Keyword_ID) as cnt
from tablename
group by Customer_ID,Keyword_ID
)

select * from cte a where cnt in (select max(cnt) from cte b where a.Customer_ID=b.Customer_ID )

答案 2 :(得分:0)

您可以尝试以下查询

 select Customer_ID,Keyword_ID,Count(Keyword_ID) as Count from tab group by 
    Customer_ID,Keyword_ID 
    Having Count(Keyword_ID)=( 
    SELECT MAX(mycount) 
    FROM ( 
    SELECT Keyword_ID, COUNT(Keyword_ID) mycount 
    FROM tab 
    GROUP BY Keyword_ID) checkMaxValue)

Click here to view the reference

答案 3 :(得分:0)

使用带有ROW_NUMBER()列的PARTITION BY Customer_ID的另一种方法。

您可以尝试以下操作。

select *
from
(
    select *, row_number() over(partition by Customer_ID order by ct desc) rn
    from
    (
    select Customer_ID , Keyword_ID, count(*) ct
    from YOURTABLE
    GROUP BY Customer_ID , Keyword_ID
    ) t
) t1
where rn=1