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
;

  

posted @ 2021-09-26 17:37  SailorG  阅读(228)  评论(0编辑  收藏  举报