如何对 sql db2-400 中的列值进行排序?

How to sort columns values in sql db2-400?

我有 10 列具有相似的信息,当大于 0 时,我想将它们升序排列。
例如,从这个:

A1 A2 A3 A4 A5 A6 A7 A8 A9 A0
00 01 00 09 08 05 00 02 06 03

为此:

A1 A2 A3 A4 A5 A6 A7 A8 A9 A0
01 02 03 05 06 08 09 00 00 00

我一直在尝试 minmax 功能,但无济于事。

您可以取消数据透视并重新聚合:一种方法是:

select max(case when seqnum = 1 then a end) as a1,
       max(case when seqnum = 2 then a end) as a2,
       . . .
from (select a.a,
             row_number() over (order by a desc) as seqnum
      from (select a1 as a from t union all
            select a2 from t union all
            select a3 from t union all
            . . .
           ) a
     ) a;
        
with
base (key, a1, a2, a3, a4, a5, a6, a7, a8, a9, a0) as (
  values
  (1, '00', '01', '00', '09', '08', '05', '00', '02', '06', '03'),
  (2, '08', '02', '00', '06', '01', '02', '00', '04', '03', '09')
),
ordered (k, value, seq) as (
  select
    key,value,rownumber() over(partition by key order by nullif(value,'00')) seq
  from base cross
    join lateral (values a1,a2,a3,a4,a5,a6,a7,a8,a9,a0) v(value)
)
select
  key,
  (select value from ordered o where (k, seq) = (key, 1)) A1,
  (select value from ordered o where (k, seq) = (key, 2)) A2,
  (select value from ordered o where (k, seq) = (key, 3)) A3,
  (select value from ordered o where (k, seq) = (key, 4)) A4,
  (select value from ordered o where (k, seq) = (key, 5)) A5,
  (select value from ordered o where (k, seq) = (key, 6)) A6,
  (select value from ordered o where (k, seq) = (key, 7)) A7,
  (select value from ordered o where (k, seq) = (key, 8)) A8,
  (select value from ordered o where (k, seq) = (key, 9)) A9,
  (select value from ordered o where (k, seq) = (key, 10)) A0
from base