Iseries SQL DBU 将行展平为列

Iseries SQL DBU Flatten Row into Columns

我有一个 iSeries 服务器 运行 V5R4。我需要将一行压平成列。这是数据示例:

Sequence    Ordn        Ordl    Percentage
1           0140766       1          0
2           0140766       1         30
3           0140766       1          7
4           0140766       1          3 
1           0140766       2          0
2           0140766       2         30
3           0140766       2          2

顺序是计算百分比的顺序
Ordn是客户的订单号
Ordl 是订单行号
百分比是标价的百分比列表

对于任何给定的订单行,行数(或百分比)可以在 1 - 5 之间变化。

文件需要按订单号分组,先按订单行号排序,然后按顺序排序。

我需要展平这个文件,以便它以下列方式显示:

Ordn    Ordl    Perc1   Perc2   Perc3   Perc4   Perc5
0140766   1       0       30      7       3      Null
0140766   2       0       30      2      Null    Null

有人可以帮忙吗?我已经尝试了几件事,但没有任何效果是我想要的,而且我的 SQL 经验非常有限。

这是一种将数据手动旋转为五个序列列的方法。它假设只有五个可能的序列。此外,如果给定序列有多于一行,百分比将相加。

select                                                            
  ordn                                                            
, ordl                                                            
, sum(case when sequence=1 then percentage else null end) as perc1
, sum(case when sequence=2 then percentage else null end) as perc2
, sum(case when sequence=3 then percentage else null end) as perc3
, sum(case when sequence=4 then percentage else null end) as perc4
, sum(case when sequence=5 then percentage else null end) as perc5
from yourtable                                              
group by ordn, ordl                                               
order by ordn, ordl       

(请注意,else null 子句在这里不是绝对必要的,因为 null 是缺少 else 大小写的 case 表达式的默认值。)