因爲你可以按照以下查詢高校的動態列表:
;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 ...這就是我要說的。 –