因为你可以按照以下查询高校的动态列表:
;with cte as (--Tally table to get list of numbers
select top (select (max(ranking)/50)+1 from youruniversity) rowN = (row_number() over (order by (select null)) *50)-50 from master..spt_values n1, master..spt_values n2
), cte2 as ( --We can avoid this cte by adding and subtracting in first cte, you can do that
select concat(RowN+1 , '-', lead(rown,1,rown+50) over (order by RowN)) as [Range], RowN+1 as Startid, lead(rown,1,rown+50) over (order by RowN) as EndId
from cte
) --Final query just by group by
select [Range] as [Ranking], count(distinct Universtities) as [No Of Universities]
from cte2 cross join youruniversity
where ranking > startid and ranking <= endid
group by [range]
输出如下:
+---------+--------------------+
| Ranking | No Of Universities |
+---------+--------------------+
| 1-50 | 2 |
| 101-150 | 2 |
| 201-250 | 1 |
| 251-300 | 1 |
| 51-100 | 3 |
+---------+--------------------+
对于如下输入:
create table youruniversity (Universtities varchar(50), Ranking int)
insert into youruniversity (Universtities, Ranking) values
('University of Arizona ', 38 )
,('Havard University ', 6 )
,('RMIT ', 213)
,('University of Sheffield', 106)
,('York University ', 111)
,('Korea University ', 63 )
,('University of Melbourne', 59 )
,('University of Waterloo ', 78 )
,('Madurai Kamaraj University', 280) --added for testing
我删除了不兼容的数据库标记。请仅使用您正在使用的数据库进行标记。 –
你在SQL Server或MySQL?另外,这看起来像功课。你已经尝试了什么?你究竟在干什么? –
Case,Sum,Count ...这就是我要说的。 –