2017-05-23 137 views
0

我對SQL有點新,並且被分組部分卡住了。我有一張大學的桌子,我想分組他們。下表中的東西看起來像,針對範圍的SQL分組查詢

Universities     Ranking 
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 

我想用這張表做一個新表。我的新表應該看起來像下面那樣,

Ranking     No of Universities 
1-50       x 
51-99       x 
100-149      x 
so on 

如何在這種情況下進行分組?特別是在第一列的範圍內。

+1

我刪除了不兼容的數據庫標記。請僅使用您正在使用的數據庫進行標記。 –

+2

你在SQL Server或MySQL?另外,這看起來像功課。你已經嘗試了什麼?你究竟在幹什麼? –

+2

Case,Sum,Count ...這就是我要說的。 –

回答

3

喜歡的東西:

SELECT CASE 
      WHEN Ranking >= 1 AND Ranking <= 50 THEN '1-50' 
      WHEN Ranking >= 51 AND Ranking <= 100 THEN '51-100' 
     END AS Range 
,  COUNT(1) AS Number 
FROM Your_Table 
GROUP BY CASE 
      WHEN Ranking >= 1 AND Ranking <= 50 THEN '1-50' 
      WHEN Ranking >= 51 AND Ranking <= 100 THEN '51-100' 
     END 
+0

您的意思是,排名<= 50? – lk1904p

+0

@ Ik1904p有一個錯誤,我現在糾正了。你的意思是?我的範圍有點不同於你提到的,但這是一般的想法 – HoneyBadger

+0

是現在得到它。非常感謝。 – lk1904p

0

因爲你可以按照以下查詢高校的動態列表:

;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 
0

你想50號範圍的羣體,所以用一些數學來獲得組:

select 
    trunc((ranking - 1)/50) * 50 + 1 as ranking_from, 
    trunc((ranking - 1)/50) * 50 + 50 as ranking_till, 
    count(*) as no_of_universities 
from mytable 
group by trunc((ranking - 1)/50) 
order by trunc((ranking - 1)/50); 
1

我建立一個測試表與您的數據。

CREATE TABLE #TmpUni (
    Uni varchar(100) 
    ,Ranking smallint); 

INSERT INTO #TmpUni 
SELECT 'University of Arizona', 38 
INSERT INTO #TmpUni 
SELECT 'Havard University', 6 
INSERT INTO #TmpUni 
SELECT 'RMIT', 100 
INSERT INTO #TmpUni 
SELECT 'University of Sheffield', 106 
INSERT INTO #TmpUni 
SELECT 'York University', 111 
INSERT INTO #TmpUni 
SELECT 'Korea University', 63 
INSERT INTO #TmpUni 
SELECT 'University of Melbourne', 59 
INSERT INTO #TmpUni 
SELECT 'University of Waterloo', 78; 

WITH CTE AS (
    SELECT 
     Ranking = CASE WHEN Ranking BETWEEN 1 AND 50 THEN '1-50' 
         WHEN Ranking BETWEEN 51 AND 99 THEN '51-99' 
         WHEN Ranking BETWEEN 100 AND 150 THEN '100 - 150' 
        END 
     ,Uni 
    FROM #TmpUni) 

SELECT DISTINCT 
    Ranking 
    ,COUNT (Uni) OVER (PARTITION BY Ranking) AS NumberOfUniversities 
FROM CTE