如何在分组中去掉两个最小值求和?
比如下面格式数据:
A    B    C   主键列略
xi   za   20 
hi   za   18
ci   za   22
bi   ha   21
fi   ha   23
mi   ha   20
...
要求得到结果如下:
B    结果
za  (20+18+22)-18-22
ha  (21+23+20)-20-21
也就是对B分组求和并去掉两个最小值。
求各位解答
谢谢

解决方案 »

  1.   

    select b,sum(c)
     from(
           select b , c ,
                  lead(num) over(partition by b order by c) max_num,
                  lead(num) over(partition by b order by c) min_num
             from (select b , c , row_number(c) over(order by b,c) num from test)
           )
      where max_num is not null and min_num is not null 
      group by b
      

  2.   

    select b,sum(c)
     from(
           select b , c ,
                  lead(num) over(partition by b order by c) max_num,
                  lag(num) over(partition by b order by c) min_num
             from (select b , c , row_number(c) over(order by b,c) num from test)
           )
      where max_num is not null and min_num is not null 
      group by b