如何在列postgres中对顶级类别进行排名
How to rank top categories in a column postgres
我需要找到学校中排名前 5 位的国籍。
Select count(distinct studentnr),
count(case when ctf.text like 'United Kingdom' then 1 end) as nation1,
count(case when ctf.text like 'Germany' then 1 end) as nation2,
count(case when ctf.text like 'France' then 1 end) as nation3,
count(case when ctf.text like 'Italy' then 1 end) as nation4,
count(case when ctf.text like 'Hungary' then 1 end) as nation5
from student s
join pupil p on p.id = s.personid
join pupilnationality pn on pn.pupilid = p.id
join country ctf on ctf.id = pn.countryid
如您所见,这是一个手动搜索,我希望我查找字段并进行计数,然后将它们单独分类在一列中。
不过我只想要前5名
这几乎就是我想要的
这需要分区或等级吗?
为什么不把它们放在不同的行中?
select ctf.text, count(*)
from student s join
pupil p
on p.id = s.personid join
pupilnationality pn
on pn.pupilid = p.id join
country ctf
on ctf.id = pn.countryid
group by ctf.text
order by count(*) desc
limit 5;
我需要找到学校中排名前 5 位的国籍。
Select count(distinct studentnr),
count(case when ctf.text like 'United Kingdom' then 1 end) as nation1,
count(case when ctf.text like 'Germany' then 1 end) as nation2,
count(case when ctf.text like 'France' then 1 end) as nation3,
count(case when ctf.text like 'Italy' then 1 end) as nation4,
count(case when ctf.text like 'Hungary' then 1 end) as nation5
from student s
join pupil p on p.id = s.personid
join pupilnationality pn on pn.pupilid = p.id
join country ctf on ctf.id = pn.countryid
如您所见,这是一个手动搜索,我希望我查找字段并进行计数,然后将它们单独分类在一列中。
不过我只想要前5名 这几乎就是我想要的 这需要分区或等级吗?
为什么不把它们放在不同的行中?
select ctf.text, count(*)
from student s join
pupil p
on p.id = s.personid join
pupilnationality pn
on pn.pupilid = p.id join
country ctf
on ctf.id = pn.countryid
group by ctf.text
order by count(*) desc
limit 5;