分组最大(案例)和(案例)

时间:2017-11-21 10:37:06

标签: sql oracle

在查询包括

的情况下
select
max(case when label in (value_1) then value else null) as "value_a"
   (case when label in (value_2) then value else null) as "value_b"
   (case when label in (value_3) then value else null) as "value_c"
   (case when label in (value_4) then value else null) as "value_d"
max(case when label in (value_5) then value else null) as "value_e"
from table;

如何使用分组来显示the link中可用文件中显示的结果?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要每列中的值列表。一种方法是使用row_number()

with t as (
      select t.*,
             dense_rank() over (order by a desc) as seqnum_a,
             dense_rank() over (order by b desc) as seqnum_b,
             dense_rank() over (order by c desc) as seqnum_c,
             dense_rank() over (order by d desc) as seqnum_d,
             dense_rank() over (order by e desc) as seqnum_e
       from t
      )
select seqnum, max(a) as a, max(b) as b, max(c) as c, max(d) as d, max(e) as e
from ((select distinct seqnum_a as seqnum, a, NULL as b, NULL as c, NULL as d, NULL as e
       from t
      ) union all
      (select distinct seqnum_b, NULL, b, NULL as c, NULL as d, NULL as e
       from t
      ) union all
      (select distinct seqnum_c, NULL, NULL as b, c, NULL as d, NULL as e
       from t
      ) union all
      (select distinct seqnum_d, NULL, NULL as b, NULL as c, d, NULL as e
       from t
      ) union all
      (select distinct seqnum_e, NULL as a, NULL as b, NULL as c, NULL as d, e
       from t
      )
     ) abcde
group by seqnum
order by seqnum;