2016-12-21 108 views
0

從表中選擇年度,代碼,基準;Oracle列行分裂成多列

結果:年,代碼,基準

2017, MKPI00719, 1 
    2017, MKPI00719, benchmark0001 
    2017, MKPI00719, 2016 
    2017, MKPI00719, 90 
    2017, MKPI00743, 1 
    2017, MKPI00743, benchmark34455 
    2017, MKPI00743, 2013 
    2017, MKPI00743, 900 

我想下面顯示這樣

Year, code,  ben_value, desc,   b_year, kpi 
2017, MKPI00719, 1,  benchmark0001, 2016, 90 
2017, MKPI00743, 1,  benchmark87899, 2013, 900 

這塞納里奧ben_value總是一個。那就是獨特的,分組編號 的Year,代碼。

請幫助我如何寫我的SQL在Oracle

+0

SQL表本質上是無序的。您重新安排的數據似乎取決於行的排序。你有另一列指定排序? –

+0

是的,得到序列號 – bab

+0

將序列號添加到您的樣本數據 - 我不明白它是如何工作的。那個序列中的典型值是什麼(對於包含的行)? – mathguy

回答

0

如果你有一個序列號,然後我覺得有以下解決您的問題:

select year, code, 
     max(case when seqnum = 1 then benchmark end) as ben_value, 
     max(case when seqnum = 2 then benchmark end) as description, 
     max(case when seqnum = 3 then benchmark end) as b_year, 
     max(case when seqnum = 4 then benchmark end) as kpi 
from (select t.*, 
      row_number() over (partition by year, code order by sequence_no) as seqnum 
     from t 
    ) t 
group by year, code 
+0

明白了,非常感謝 – bab