我的表是:
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
规则:
答案 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
或其他构造进行排序。