2015-02-10 27 views
1

我有運行V5R4的iSeries服務器。我需要將一行變平。這裏的數據的一個示例:Iseries SQL DBU將行拼合成列

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體驗非常有限。

回答

1

以下是手動將數據轉換爲五個序列列的方法。它假定只有五個可能的序列。此外,如果您有多個具有給定序列的行,則會將百分比加在一起。

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條款是不是絕對必要在這裏,因爲nullcase表達缺乏的else箱子默認值。)

+0

那完美!非常感謝你的幫助。 – 2015-02-10 15:35:26