行轉(zhuǎn)列的準則就是通過主鍵進行分組,之后對行其它字段加上sum()、max()、count()函數(shù),里邊用decode()這類函數(shù)進行處理,總之分組不要用到他就成。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比羅源網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式羅源網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋羅源地區(qū)。費用合理售后完善,十載實體公司更值得信賴。
姓名 數(shù)學(xué) 語文 姓名 張飛 趙云 龐統(tǒng)
張飛 60 61 數(shù)學(xué) 60 80 99
趙云 80 85 變成 語文 61 85 80
龐統(tǒng) 99 80
而且第一行為列名,不是表中的數(shù)據(jù),這樣的話
也就是第一個表下面三行的數(shù)據(jù),變成第二個表下面兩行的數(shù)據(jù)
這樣還是可以的
create?table?A(名稱?varchar2(10),"1月"?varchar2(10),"2月"?varchar2(10),"3月"?varchar2(10),"4月"?varchar2(10))
insert?into?A?values?('土豆',50,60,70,80);
insert?into?A?values?('白菜',150,160,170,180);
commit;
select?*?from?A;
select?*?from?(
select?名稱,'1月'?月份,"1月"?值?from?A
union?all
select?名稱,'2月'?月份,"2月"?值?from?A
union?all
select?名稱,'3月'?月份,"3月"?值?from?A
union?all
select?名稱,'4月'?月份,"4月"?值?from?A)
where?名稱='土豆'
結(jié)果:
用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 ...
測試表測了下,可以用,你看看,主要是先根據(jù)逗號進行分割,然后connect,level等于逗號的數(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;
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