java转置的⽅法_总结⼀下关于⾏列转置的实现⽅法(CSDN)
1、固定列数的⾏列转换
如
student subject grade--------- ---------- --------student1 语⽂80student1 数学70student1 英语60student2 语⽂90student2 数学
80student2 英语100……
转换为
语⽂ 数学 英语
student1807060student29080100……
语句如下:selectstudent,sum(decode(subject,'语⽂', grade,null)) "语⽂",sum(decode(subject,'数学', grade,null)) "数
学",sum(decode(subject,'英语', grade,null)) "英语"fromtablegroupbystudent;2、不定列⾏列转换
如
c1 c2--- -----------1我1是1谁2知2道3不
……
转换为1我是谁2知道3不
这⼀类型的转换可以借助于PL/SQL来完成,这⾥给⼀个例⼦
CREATEORREPLACEFUNCTIONget_c2(tmp_c1NUMBER)RETURNVARCHAR2ISCol_c2VARCHAR2(4000);BEGINFORcurIN(SELECT LOOP
Col_c2 :=Col_c2||cur.c2;ENDLOOP;
Col_c2 :=rtrim(Col_c2,1);RETURNCol_c2;END;selectdistinctc1 ,get_c2(c1) cc2fromtable;
或者不⽤pl/sql,利⽤分析函数和 CONNECT_BY 实现:SELECTc1, SUBSTR (MAX(SYS_CONNECT_BY_PATH (c2,';')),2)
NAMEFROM(SELECTc1, c2, rn, LEAD (rn)OVER(PARTITIONBYc1ORDERBYrn) rn1FROM(SELECTc1, c2, ROW_NUMBER
()OVER(ORDERBYc2) rnFROMt))
java replace方法STARTWITHrn1ISNULLCONNECTBYrn1=PRIOR rnGROUPBYc1;3、列数不固定(交叉表⾏列转置)
这种是⽐较⿇烦的⼀种,需要借助pl/sql:
原始数据:
CLASS1 CALLDATE CALLCOUNT12005-08-084012005-08-07622005-08-087732005-08-093332005-08-
08932005-08-0721转置后:
CALLDATE CallCount1 CallCount2 CallCount3------------ ---------- ---------- ----------2005-08-0900332005-08-
0840*******-08-076021试验如下:1). 建⽴测试表和数据CREATETABLEt(
class1VARCHAR2(2BYTE),
calldate DATE,
callcountINTEGER);INSERTINTOt(class1, calldate, callcount)VALUES('1', TO_DATE
('08/08/2005','MM/DD/YYYY'),40);INSERTINTOt(class1, calldate, callcount)VALUES('1', TO_DATE
('08/07/2005','MM/DD/YYYY'),6);INSERTINTOt(class1, calldate, callcount)VALUES('2', TO_DATE
('08/08/2005','MM/DD/YYYY'),77);INSERTINTOt(class1, calldate, callcount)VALUES('3', TO_DATE
('08/09/2005','MM/DD/YYYY'),33);INSERTINTOt(class1, calldate, callcount)VALUES('3', TO_DATE
('08/08/2005','MM/DD/YYYY'),9);INSERTINTOt(class1, calldate, callcount)VALUES('3', TO_DATE
('08/07/2005','MM/DD/YYYY'),21);COMMIT;2). 建⽴ref cursor准备输出结果集CREATEORREPLACEPACKAGE
pkg_getrecordISTYPE myrctypeISREFCURSOR;ENDpkg_getrecord;/3). 建⽴动态sql交叉表函数,输出结果集CREATEORREPLACEFUNCTIONfn_peISsVARCHAR2(4
000);CURSORc1ISSELECT',sum(case when Class1='||class1||'then CallCount else 0 end)'||'"CallCount'||class1||'"'c2FROMtGROUPBYclass1;
r1 c1%ROWTYPE;
list_cursor pe;BEGINs :='select CallDate';OPENc1;
LOOPFETCHc1INTOr1;EXITWHENc1%NOTFOUND;
s :=s||r1.c2;ENDLOOP;CLOSEc1;
s :=s||'from T group by CallDate order by CallDate desc';OPENlist_cursorFORs;RETURNlist_cursor;ENDfn_rs;/4). 测试在sql plus下执⾏:varresults refcursor;exec:results :=fn_rs;printresults;
CALLDATE CallCount1 CallCount2 CallCount3--------------- ---------- ---------- ----------2005-08-0900332005-08-0840*******-08-076021
posted on 2007-12-03 17:06 都市淘沙者 阅读(631) 评论(0) 编辑 收藏 所属分类: Oracle/Mysql/Postgres/
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论