选择前5个组并按顺序排列

时间:2018-04-06 03:09:24

标签: mysql sql sql-server

我有一张这样的表:

CITY                            QNT EXP RATE
LONDON                          60  6   900
LONDON                          35  8   337
LONDON                          24  6   300
LONDON                          22  6   266
BIRMINGHAM                      22  6   266
NEWYORK                         69  19  263
LONDON                          21  6   250
ROME                            24  7   242
BIRMINGHAM                      24  7   242
BIRMINGHAM                      24  7   242
LONDON                          20  6   233
BIRMINGHAM                      23  7   228
STUTTGART                       29  9   222
LONDON                          19  6   216
STUTTGART                       25  8   212
PARIS                           31  10  210
STUTTGART                       34  11  209
STUTTGART                       34  11  209
BIRMINGHAM                      18  6   200
BIRMINGHAM                      18  6   200
NEWYORK                         18  6   200
BIRMINGHAM                      17  6   183
LONDON                          19  7   171
MUNICH                          16  6   166
PARIS                           21  8   162
STUTTGART                       39  15  160
BARCELONA                       18  7   157
LONDON                          18  7   157
ROME                            33  13  153
BARCELONA                       15  6   150
PARIS                           25  10  150
ROME                            20  8   150
PARIS                           25  10  150
ROME                            20  8   150
LONDON                          15  6   150
MUNICH                          15  6   150
BIRMINGHAM                      15  6   150
NEWYORK                         15  6   150
LONDON                          17  7   142
MUNICH                          17  7   142

这是我的sql命令:

select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city
order by RATE desc 

我想按城市分组这些结果。但我无法做到。我希望top5行改变最多。

结果应该是这样的:

LONDON                          60  6   900
BIRMINGHAM                      22  6   266
NEWYORK                         69  19  263
ROME                            24  7   242
STUTTGART                       29  9   222

3 个答案:

答案 0 :(得分:3)

这来自 MySQL 5.6。

select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
from tbl_city GROUP BY CITY
order by RATE desc LIMIT 5

在此链接中进行了测试:http://www.sqlfiddle.com/#!9/3f1ea1/1

这是来自 MS SQL Server 2017

select TOP 5 f.CITY, f.QNT, f.EXP, x.RATE
from (
select  CITY, MAX((QNT-EXP)*100/EXP) as RATE
from tbl_city GROUP BY CITY
) as x inner join tbl_city as f on f.CITY = x.CITY 
and ((f.QNT-f.EXP)*100/f.EXP) = x.RATE
ORDER BY RATE DESC;

在此链接中进行了测试:http://www.sqlfiddle.com/#!18/7b8da/61

答案 1 :(得分:2)

也许你想要这样的东西?

select top 5 CITY, QNT, EXP, RATE 
from (
    select *, row_number() over (partition by CITY order by RATE desc) AS RN
    from (
        select CITY, QNT, EXP, (QNT-EXP)*100/EXP as RATE
        from tbl_city

    ) X
) Y
where RN = 1
order by RATE desc

我没有对此进行测试,但它应首先采用最快速率的城市行,然后排在前5行,以便同一城市不重复

答案 2 :(得分:0)

我不知道指定公式背后的逻辑是什么,但这可以做你想要的

select top 5 * from (
    select top(1) with ties city, qnt, exp, (qnt-exp)*100/exp as rate 
    from tbl_city 
    order by row_number() over (partition by city order by (qnt-exp)*100/exp desc)
)t
order by rate desc

但是,SQL Server中提供了 top(1)with ties 和分析函数。