Oracle行转列/列转行
1.oracle的pivot函数 原表 使用pivot函数: with temp as( 说明:pivot(聚合函数?for?列名?in(类型)),其中?in(‘’)?中可以指定别名,in中还可以指定子查询,比如?selectdistinct ranking?from?temp SELECT * FROM [StudentScores] /*数据源*/ AS P PIVOT ( SUM(Score/*行转列后 列的值*/) FOR p.Subject/*需要行转列的列*/ IN ([语文],[数学],[英语],[生物]/*列的值*/) ) AS T ? 2.使用max结合decode函数 原表 使用max结合decode函数 with temp as( 说明:decode的用法:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下: IF 条件=值1 THEN RETURN(翻译值1) ELSIF 条件=值2 THEN RETURN(翻译值2) ...... ELSIF 条件=值n THEN RETURN(翻译值n) ELSE RETURN(缺省值) END IF ? 3.使用max结合case when 函数 原表 使用max结合case when 函数 select case when grade_id=‘1‘ then ‘一年级‘ when grade_id=‘2‘ then ‘二年级‘ when grade_id=‘5‘ then ‘五年级‘? else null end "年级", max(case when subject_name=‘语文‘? then max_score else 0 end) "语文", max(case when subject_name=‘数学‘? then max_score else 0 end) "数学", max(case when subject_name=‘政治‘? then max_score else 0 end) "政治" from dim_ia_test_ysf group by case when grade_id=‘1‘ then ‘一年级‘ when grade_id=‘2‘ then ‘二年级‘ when grade_id=‘5‘ then ‘五年级‘? else null end? ? ? 下面是介绍列转行方法--列转行 原表 with temp as( (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |