行轉(zhuǎn)列的準(zhǔn)則就是通過(guò)主鍵進(jìn)行分組,之后對(duì)行其它字段加上sum()、max()、count()函數(shù),里邊用decode()這類(lèi)函數(shù)進(jìn)行處理,總之分組不要用到他就成。
成都創(chuàng)新互聯(lián)專(zhuān)注于烏拉特前網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供烏拉特前營(yíng)銷(xiāo)型網(wǎng)站建設(shè),烏拉特前網(wǎng)站制作、烏拉特前網(wǎng)頁(yè)設(shè)計(jì)、烏拉特前網(wǎng)站官網(wǎng)定制、微信小程序開(kāi)發(fā)服務(wù),打造烏拉特前網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供烏拉特前網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。
with??t(cate_displayname,info_waritername,count)?as?(
select?'2014年','lizifeng',1?from?dual?
union?all
select?'歷史沿革','wenqingwen',20?from?dual?
union?all
select?'領(lǐng)導(dǎo)講話','lizifeng',3?from?dual?
)
SELECT?cate_displayname,
nvl(lizifeng,0)?as?lizifeng,
nvl(wenqingwen,0)?as?wenqingwen
FROM???t
PIVOT?(
sum(count)????????----?pivot_clause
FOR?info_waritername??????????----?pivot_for_clause
IN??('lizifeng'?as?lizifeng,'wenqingwen'?as?wenqingwen)???----?pivot_in_clause
);
輸出:
CATE_DISPLAYNAME???????????LIZIFENG?WENQINGWEN
------------------------?----------?----------
2014年????????????????????????????1??????????0
歷史沿革??????????????????????????0?????????20
領(lǐng)導(dǎo)講話??????????????????????????3??????????0
用union all
假設(shè)列名分別為 col1 cola colb...
select col1,cola
from tabname
where ...
union all
select col1,colb
from tabname
where ...
union all
select col1,colc
from tabname
where ...
union all
select col1,cold
from tabname
where ...
union all
select col1,cole
from tabname
where ...
union all
select col1,colf
from tabname
where ...
測(cè)試表測(cè)了下,可以用,你看看,主要是先根據(jù)逗號(hào)進(jìn)行分割,然后connect,level等于逗號(hào)的數(shù)目:
select id,nvl(substr(glbh,instr(glbh,',',1,lvl)+1,instr(glbh,',',1,lvl+1)-instr(glbh,',',1,lvl)-1),'kong') glbh
from (
select id,lvl,','||glbh||',' glbh
from test a ,
(select level lvl from dual connect by level=
(select max(length(glbh)-length(replace(glbh,',')))+1 from test)) b
) t1
where substr(glbh,instr(glbh,',',1,lvl)+1,instr(glbh,',',1,lvl+1)-instr(glbh,',',1,lvl)-1) is not null
order by id,glbh;
姓名 數(shù)學(xué) 語(yǔ)文 姓名 張飛 趙云 龐統(tǒng)
張飛 60 61 數(shù)學(xué) 60 80 99
趙云 80 85 變成 語(yǔ)文 61 85 80
龐統(tǒng) 99 80
而且第一行為列名,不是表中的數(shù)據(jù),這樣的話
也就是第一個(gè)表下面三行的數(shù)據(jù),變成第二個(gè)表下面兩行的數(shù)據(jù)
這樣還是可以的