MSSQL:使用列中最高单词的数量

时间:2018-05-22 08:25:08

标签: sql sql-server

我的表是:

areaid  rank
1   Positive
1   Neutral
1   Positive
1   Positive
1   Positive
1   Negative
2   Positive
2   
2   Positive
1    
1   Positive
1   Positive
1    
2   Negative
2   Positive

我正在寻找关注输出

areaid    rank
1    positive
2    positive

规则:

  1. 每个areaid应具有最大出现次数的等级。
  2. 如果阳性发生次数等于阴性,阳性次数大于中性则结果应为阴性。
  3. 因此优先检查发生次数是否相等:优先级:负面,中性和最后一次正面

2 个答案:

答案 0 :(得分:1)

试试这个:

select 
    t.areaid,
    case 
        when cnt_neg>=cnt_neut and cnt_neg>=cnt_pos then 'Negative'
        when cnt_neut>=cnt_neg then 'Neutral'
        else 'Positive'
    end as rank
FROM
(
    select areaid,
    sum(case when rank='Positive' then 1 else 0 end) as cnt_pos,
    sum(case when rank is null or rank not in ('Positive','Negative') then 1 else 0 end) as cnt_neut,
    sum(case when rank='Negative' then 1 else 0 end) as cnt_neg
    from yourtable
    group by areaid
)t

答案 1 :(得分:1)

我会这样做:

select t.*
from (select areaid, rank, count(*) as cnt,
             row_number() over (partition by areaid
                                order by count(*) desc, rank asc
                               ) as seqnum
      from t
      group by areaid, rank
     ) t
where seqnum = 1;

rank asc中的order by有点像黑客,但您的优先级是字母。或者,您可以使用case或其他构造进行排序。