[一]、行转列
1.1、初始测试数据
表结构:TEST_TB_GRADE
create
tableTEST_TB_GRADE
(
IDNUMBER(10)notnull,
USER_NAMEVARCHAR2(20CHAR),
COURSEVARCHAR2(20CHAR),
SCOREFLOAT
)
createtableTEST_TB_GRADE
(
IDNUMBER(10)notnull,
USER_NAMEVARCHAR2(20CHAR),
COURSEVARCHAR2(20CHAR),
SCOREFLOAT
)
初始数据如下图:
1.2、 如果需要实现如下的查询效果图:
这就是最常见的行转列,主要原理是利用decode函数、聚集函数(sum),结合group by分组实现的,具体的sql如下:
selectt.user_name,
sum(decode(t.course,'语文',score,null))asCHINESE,
sum(decode(t.course,'数学',score,null))asMATH,
sum(decode(t.course,'英语',score,null))asENGLISH
fromtest_tb_gradet
groupbyt.user_name
orderbyt.user_name
selectt.user_name,
sum(decode(t.course,'语文',score,null))asCHINESE,
sum(decode(t.course,'数学',score,null))asMATH,
sum(decode(t.course,'英语',score,null))asENGLISH
fromtest_tb_gradet
groupbyt.user_name
orderbyt.user_name
1.3、延伸
如果要实现对各门功课的不同分数段进行统计,效果图如下:
具体的实现sql如下:
selectt2.SCORE_GP,
sum(decode(t2.course,'语文',COUNTNUM,null))asCHINESE,
sum(decode(t2.course,'数学',COUNTNUM,null))asMATH,
sum(decode(t2.course,'英语',COUNTNUM,null))asENGLISH
from(
selectt.course,
casewhent.score<60then'00-60'
whent.score>=60andt.score<80then'60-80'
whent.score>=80then'80-100'endasSCORE_GP,
count(t.score)asCOUNTNUM
FROMtest_tb_gradet
groupbyt.course,
casewhent.score<60then'00-60'
whent.score>=60andt.score<80then'60-80'
whent.score>=80then'80-100'end
orderbyt.course)t2
groupbyt2.SCORE_GP
orderbyt2.SCORE_GP
selectt2.SCORE_GP,
sum(decode(t2.course,'语文',COUNTNUM,null))asCHINESE,
sum(decode(t2.course,'数学',COUNTNUM,null))asMATH,
sum(decode(t2.course,'英语',COUNTNUM,null))asENGLISH
from(
selectt.course,
casewhent.score<60then'00-60'
whent.score>=60andt.score<80then'60-80'
whent.score>=80then'80-100'endasSCORE_GP,
count(t.score)asCOUNTNUM
FROMtest_tb_gradet
groupbyt.course,
casewhent.score<60then'00-60'
whent.score>=60andt.score<80then'60-80'
whent.score>=80then'80-100'end
orderbyt.course)t2
groupbyt2.SCORE_GP
orderbyt2.SCORE_GP
[二]、列转行
1.1、初始测试数据
表结构:TEST_TB_GRADE2
create
tableTEST_TB_GRADE2
(
IDNUMBER(10)notnull,
USER_NAMEVARCHAR2(20CHAR),
CN_SCOREFLOAT,
MATH_SCOREFLOAT,
EN_SCOREFLOAT
)
createtableTEST_TB_GRADE2
(
IDNUMBER(10)notnull,
USER_NAMEVARCHAR2(20CHAR),
CN_SCOREFLOAT,
MATH_SCOREFLOAT,
EN_SCOREFLOAT
)
初始数据如下图:
1.2、 如果需要实现如下的查询效果图:
这就是最常见的列转行,主要原理是利用SQL里面的union,具体的sql语句如下:
selectuser_name,
'语文'COURSE,CN_SCORE
asSCORE
fromtest_tb_grade2
unionselectuser_name,'数学'COURSE,MATH_SCOREasSCOREfromtest_tb_grade2
unionselectuser_name,'英语'COURSE,EN_SCOREasSCOREfromtest_tb_grade2
orderbyuser_name,COURSE
selectuser_name,'语文'COURSE,CN_SCOREasSCOREfromtest_tb_grade2
unionselectuser_name,'数学'COURSE,MATH_SCOREasSCOREfromtest_tb_grade2
unionselectuser_name,'英语'COURSE,EN_SCOREasSCOREfromtest_tb_grade2
orderbyuser_name,COURSE
也可以利用【insert all into ... select】来实现,首先需要先建一个表TEST_TB_GRADE3:
create
tableTEST_TB_GRADE3
(
USER_NAMEVARCHAR2(20CHAR),
COURSEVARCHAR2(20CHAR),
SCOREFLOAT
)
createtableTEST_TB_GRADE3
(
USER_NAMEVARCHAR2(20CHAR),
COURSEVARCHAR2(20CHAR),
SCOREFLOAT
)
再执行下面的sql:
insert
all
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'语文',CN_SCORE)
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'数学',MATH_SCORE)
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'英语',EN_SCORE)
selectuser_name,CN_SCORE,MATH_SCORE,EN_SCOREfromtest_tb_grade2;
commit;
insertall
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'语文',CN_SCORE)
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'数学',MATH_SCORE)
intotest_tb_grade3(USER_NAME,COURSE,SCORE)values(user_name,'英语',EN_SCORE)
selectuser_name,CN_SCORE,MATH_SCORE,EN_SCOREfromtest_tb_grade2;
commit;
别忘记commit操作,然后再查询TEST_TB_GRADE3,发现表中的数据就是列转成行了。
如果觉得《怎么给oracle数据库列转行 oracle数据库 行转列 列转行详解》对你有帮助,请点赞、收藏,并留下你的观点哦!