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( (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |