范围的SQL分组查询

时间:2017-05-23 14:09:32

标签: sql oracle

我对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

在这种情况下如何进行分组?尤其是第一列中的范围。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 

答案 2 :(得分: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

答案 3 :(得分: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);