标签:sql 返回值 size back rom ble 列表 lap from
列字段的处理 SQL Case
对列做处理 SQL> select deptno,sum(sal) from emp group by deptno;
Select case when deptno=10 then ‘ACCOUNTING‘ when deptno=20 then ‘RESERCH‘ when deptno=30 then ‘SALES‘ end, sum(sal) from emp group by deptno
|
列字段的处理 SQL Decode
还有一种情况是当什么都不是的情况下,用一个值代替 前言 DECODE()函数,它将输入数值与函数中的参数列表相比较,根据输入值返回一个对应值。函数的参数列表是由若干数值及其对应结果值组成的若干序偶形式。当然,如果未能与任何一个实参序偶匹配成功,则函数也有默认的返回值。 语法结构如下: decode (expression, search_1, result_1) decode (expression, search_1, result_1, default) 比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。 select decode(deptno, 10, ‘ACCOUNTING‘, 20, ‘RESERCH‘, 30, ‘SALES‘ ), sum(sal) from emp group by deptno
|
行转列 SQL> select job,ename,sal from emp where job=‘MANAGER‘;
JOB ENAME SAL --------- ---------- ---------- MANAGER JONES 2975 MANAGER BLAKE 2850 MANAGER CLARK 2450
SQL>
SQL> select job,decode(ename,‘BLAKE‘,SAL) BLAKE,decode(ename,‘JONES‘,SAL) JONES,decode(ename,‘CLARK‘,SAL) CLARK from emp where job=‘MANAGER‘;
JOB BLAKE JONES CLARK --------- ---------- ---------- ---------- MANAGER 2975 MANAGER 2850 MANAGER 2450
SQL> select job,sum(decode(ename,‘BLAKE‘,SAL)) BLAKE,sum(decode(ename,‘JONES‘,SAL)) JONES,sum(decode(ename,‘CLARK‘,SAL)) CLARK from emp where job=‘MANAGER‘ group by job;
JOB BLAKE JONES CLARK --------- ---------- ---------- ---------- MANAGER 2850 2975 2450
SQL> select job,avg(decode(ename,‘BLAKE‘,SAL)) BLAKE,max(decode(ename,‘JONES‘,SAL)) JONES,min(decode(ename,‘CLARK‘,SAL)) CLARK from emp where job=‘MANAGER‘ group by job;
JOB BLAKE JONES CLARK --------- ---------- ---------- ---------- MANAGER 2850 2975 2450
|
SQL Fundamentals SQL列字段的处理 SQL(case、decode、行转列)
标签:sql 返回值 size back rom ble 列表 lap from
原文地址:http://www.cnblogs.com/thescentedpath/p/CASEDECODE.html