: 아래
;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에 있습니까? 또한 숙제처럼 보입니다. 당신은 이미 무엇을 시도 했습니까? 너 정확히 뭐야? –
사례, 합계, 백작 ... 그게 전부입니다. –