mysql代码实现hive中的rank()排序方法,获得top3中的最小值

-- 方式一
-- 使用判断获取top3的最小值
select case when countNum = 1 then  (select order_amount from test.test04 group by order_amount order by order_amount desc limit 1)
            when countNum = 2 then  (select order_amount from test.test04 group by order_amount order by order_amount desc limit 1,1)
            when countNum >=3 then  (select order_amount from test.test04 group by order_amount order by order_amount desc limit 2,1)
       end as order_amount1
from (
        select count(*) as countNum
        from (  select shengfen,order_amount
                from test.test04 t1
                group by order_amount
                order by order_amount desc
             ) t1
         ) t2
;

-- 方式二
-- 第一次使用倒序排序取前3
-- 第二次使用正序排序取第一,从而获取前三中的最小的值
select order_amount
from (
        select *
        from test.test02
        group by order_amount
        order by order_amount desc
        limit 3
     ) t1
order by order_amount
limit 1
;

  

原文地址:https://www.cnblogs.com/gzd1-sailoring/p/15339222.html