oracle 乙個有趣的行列轉換問題

2021-06-02 00:12:24 字數 1394 閱讀 5599

//資料

aid name  

1 test1

2 test2

3 test3

bid name code  

11 test1 '001'  

12 test1 '002'  

13 test2 '004'  

//結果

a.id b.code  

1 '001'  

2 '004'  

3 null

//方法一:先左連線,在去掉重複的資料

with ta as(

select 1 id,'test1' name from dual union all

select 2,'test2' from dual union all

select 3,'test3' from dual)

,tb as(

select 11 id,'test1' name,'001' code from dual union all

select 12,'test1','002' from dual union all

select 13,'test2','004' from dual)

select tc.id,nvl(tc.code,'null') code

from (

select ta.id,ta.name,tb.code

from ta left join tb

on ta.name=tb.name) tc

where not exists(

select 1

from tb

where tb.name=tc.name

and tb.code < tc.code)

//方法二:先去掉重複的資料,在左連線

select ta.id,b.code

from ta left join

(select *

from tb b1

where not exists(

select *

from tb b2

where b1.name=b2.name

and b1.code > b2.code)) b

on ta.name=b.name

//方法三:左連線,按照name分組,為每一小組的一行返回乙個行號,

//然後去行號為1的行

select id,code

from (

select row_number() over (partition by ta.id order by ta.id) rn

,ta.id,tb.code

from ta,tb

where ta.name=tb.name(+))

where rn=1

Oracle乙個典型行列轉換的幾種實現方法

假如有如下表,其中各個i值對應的行數是不定的 sql select from t i a d 1 b 2008 03 27 10 55 42 1 a 2008 03 27 10 55 46 1 d 2008 03 27 10 55 30 2 z 2008 03 27 10 55 55 2 t 200...

Oracle的行列轉換

b 行轉列 b 1.列固定的情況,通過max decode變換。2.列不固定的時候,通過自定義function轉換。3.通過層次查詢,將行轉換成字串。oracle 9i中沒有connect by isleaf,可以使用分析函式實現 select n.tid,max n.typename keep d...

bmp 轉換 txt 乙個有有趣的演算法

影象處理 bmp 轉換 txt 乙個有有趣的演算法 把位 件轉換成和圖案很相似的字元文字。其實原理很簡單,用到了和圖案化技術類似的思想 首先將位圖分成同樣大小的小塊,求出每一塊灰度的平均值,然後和每個字元的灰度做比較,找出最接近的那個字元,來代表這一小塊圖象。那麼,怎麼確定字元的灰度呢?做下面的實驗...