oracel查询1

--======================

--author:yeeXun

--date: 2010-11-26 10:35:00

--address:17-304

--======================

ConnectedtoOracleDatabase10gEnterpriseEditionRelease10.2.0.1.0

Connectedasscott

SQL>selectdeptno,avg(sal) fromempgroupby deptno;

DEPTNOAVG(SAL)

----------------

301566.66666

202175

102916.66666

SQL>select*fromemp;

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO

----------------------------------------------------------------

7369SMITHCLERK79021980-12-17800.0020

7499ALLENSALESMAN76981981-2-201600.00300.0030

7521WARDSALESMAN76981981-2-221250.00500.0030

7566JONESMANAGER78391981-4-22975.0020

7654MARTINSALESMAN76981981-9-281250.001400.0030

7698BLAKEMANAGER78391981-5-12850.0030

7782CLARKMANAGER78391981-6-92450.0010

7788SCOTTANALYST75661987-4-193000.0020

7839KINGPRESIDENT1981-11-175000.0010

7844TURNERSALESMAN76981981-9-81500.000.0030

7876ADAMSCLERK77881987-5-231100.0020

7900JAMESCLERK76981981-12-3950.0030

7902FORDANALYST75661981-12-33000.0020

7934MILLERCLERK77821982-1-231300.0010

14rowsselected

SQL>selecta.ename,a.sal,a.deptno,b.avgsalfroma,(selectdeptno,avg(sal)avgsalfromempgroupbydeptno)bwherea.deptno=b.deptnoanda.sal>b.avgsal;

selecta.ename,a.sal,a.deptno,b.avgsalfroma,(selectdeptno,avg(sal)avgsalfromempgroupbydeptno)bwherea.deptno=b.deptnoanda.sal>b.avgsal

ORA-00942:表或视图不存在

--表视图:只存在于查询期间,指定别名不能用“as”关键字,如果指定列的别名可以使用关键字“as”

SQL>selecta.ename,a.sal,a.deptno,b.avgsalfromempa,(selectdeptno,avg(sal)avgsalfromempgroupbydeptno)b wherea.deptno=b.deptnoanda.sal>b.avgsal;

ENAMESALDEPTNOAVGSAL

-----------------------------------

ALLEN1600.00301566.66666

JONES2975.00202175

BLAKE2850.00301566.66666

SCOTT3000.00202175

KING5000.00102916.66666

FORD3000.00202175

6rowsselected

SQL>select*fromemp;

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO

----------------------------------------------------------------

7369SMITHCLERK79021980-12-17800.0020

7499ALLENSALESMAN76981981-2-201600.00300.0030

7521WARDSALESMAN76981981-2-221250.00500.0030

7566JONESMANAGER78391981-4-22975.0020

7654MARTINSALESMAN76981981-9-281250.001400.0030

7698BLAKEMANAGER78391981-5-12850.0030

7782CLARKMANAGER78391981-6-92450.0010

7788SCOTTANALYST75661987-4-193000.0020

7839KINGPRESIDENT1981-11-175000.0010

7844TURNERSALESMAN76981981-9-81500.000.0030

7876ADAMSCLERK77881987-5-231100.0020

7900JAMESCLERK76981981-12-3950.0030

7902FORDANALYST75661981-12-33000.0020

7934MILLERCLERK77821982-1-231300.0010

14rowsselected

--分页,

SQL>selecta.*,rownumru from(select*fromemp)a;

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNORU

--------------------------------------------------------------------------

7369SMITHCLERK79021980-12-17800.00201

7499ALLENSALESMAN76981981-2-201600.00300.00302

7521WARDSALESMAN76981981-2-221250.00500.00303

7566JONESMANAGER78391981-4-22975.00204

7654MARTINSALESMAN76981981-9-281250.001400.00305

7698BLAKEMANAGER78391981-5-12850.00306

7782CLARKMANAGER78391981-6-92450.00107

7788SCOTTANALYST75661987-4-193000.00208

7839KINGPRESIDENT1981-11-175000.00109

7844TURNERSALESMAN76981981-9-81500.000.003010

7876ADAMSCLERK77881987-5-231100.002011

7900JAMESCLERK76981981-12-3950.003012

7902FORDANALYST75661981-12-33000.002013

7934MILLERCLERK77821982-1-231300.001014

14rowsselected

SQL>selecta.*,rownumrufrom(select*fromemp)awhereru<=10;

selecta.*,rownumrufrom(select*fromemp)awhereru<=10

ORA-00904:"RU":标识符无效

-- 先找出指定上限范围内的数据

SQL>selecta.*,rownumru from(select*fromemp )awhererownum<=10;

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNORU

--------------------------------------------------------------------------

7369SMITHCLERK79021980-12-17800.00201

7499ALLENSALESMAN76981981-2-201600.00300.00302

7521WARDSALESMAN76981981-2-221250.00500.00303

7566JONESMANAGER78391981-4-22975.00204

7654MARTINSALESMAN76981981-9-281250.001400.00305

7698BLAKEMANAGER78391981-5-12850.00306

7782CLARKMANAGER78391981-6-92450.00107

7788SCOTTANALYST75661987-4-193000.00208

7839KINGPRESIDENT1981-11-175000.00109

7844TURNERSALESMAN76981981-9-81500.000.003010

10rowsselected

--再指定下限范围内的数据

SQL>select*from(selecta.*,rownumru from(select*fromemp)awhererownum<=10 )whereru>=6 ;

EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNORU

--------------------------------------------------------------------------

7698BLAKEMANAGER78391981-5-12850.00306

7782CLARKMANAGER78391981-6-92450.00107

7788SCOTTANALYST75661987-4-193000.00208

7839KINGPRESIDENT1981-11-175000.00109

7844TURNERSALESMAN76981981-9-81500.000.003010


--修改显示数据,只需要修改最里层的查询即可

SQL>select*from(selecta.*,rownumrufrom(selectename,sal fromemp)awhererownum<=10 )whereru>=6 ;

ENAMESALRU

-----------------------------

BLAKE2850.006

CLARK2450.007

SCOTT3000.008

KING5000.009

TURNER1500.0010

SQL>select*from(selecta.*,rownumrufrom(selectename,salfromemporderbysal)awhererownum<=10)whereru>=6;

ENAMESALRU

-----------------------------

MILLER1300.006

TURNER1500.007

ALLEN1600.008

CLARK2450.009

BLAKE2850.0010

SQL>selectcount(*) fromemp;

COUNT(*)

----------

14

SQL>createtablemyemp(id,ename,sal)asselectempno,ename,salfrome,mp;

createtablemyemp(id,ename,sal)asselectempno,ename,salfrome,mp

ORA-00942:表或视图不存在

--数据复制法

SQL>createtablemyemp(id,ename,sal)asselect empno,ename,salfromemp ;

Tablecreated

SQL>descmyemp;

NameTypeNullableDefaultComments

----------------------------------------

IDNUMBER(4)Y

ENAMEVARCHAR2(10)Y

SALNUMBER(7,2)Y

SQL>select*frommyemp;

IDENAMESAL

------------------------

7369SMITH800.00

7499ALLEN1600.00

7521WARD1250.00

7566JONES2975.00

7654MARTIN1250.00

7698BLAKE2850.00

7782CLARK2450.00

7788SCOTT3000.00

7839KING5000.00

7844TURNER1500.00

7876ADAMS1100.00

7900JAMES950.00

7902FORD3000.00

7934MILLER1300.00

14rowsselected

SQL>selectename,sal,jobfromempwheresal> 2500;

ENAMESALJOB

----------------------------

JONES2975.00MANAGER

BLAKE2850.00MANAGER

SCOTT3000.00ANALYST

KING5000.00PRESIDENT

FORD3000.00ANALYST

SQL>selectename,sal,jobfromempwheresal>2500union

2selectename,sal,jobfromempwherejon='manager';

selectename,sal,jobfromempwheresal>2500union

selectename,sal,jobfromempwherejon='manager'

ORA-00904:"JON":标识符无效

SQL>

SQL>selectename,sal,jobfromempwheresal>2500union

2selectename,sal,jobfromempwherejon='manager'

SQL>

SQL>selectename,sal,jobfromempwheresal>2500union

2selectename,sal,jobfromempwherejob='manager';

ENAMESALJOB

----------------------------

BLAKE2850.00MANAGER

FORD3000.00ANALYST

JONES2975.00MANAGER

KING5000.00PRESIDENT

SCOTT3000.00ANALYST

SQL>selectename,sal,jobfromempwheresal>2500union

2selectename,sal,jobfromempwherejob='MANAGER';

ENAMESALJOB

----------------------------

BLAKE2850.00MANAGER

CLARK2450.00MANAGER

FORD3000.00ANALYST

JONES2975.00MANAGER

KING5000.00PRESIDENT

SCOTT3000.00ANALYST

6rowsselected

SQL>selectename,sal,jobfromempunionall

2selectename,sal,jobfromempwherejob='MANAGER';

ENAMESALJOB

----------------------------

SMITH800.00CLERK

ALLEN1600.00SALESMAN

WARD1250.00SALESMAN

JONES2975.00MANAGER

MARTIN1250.00SALESMAN

BLAKE2850.00MANAGER

CLARK2450.00MANAGER

SCOTT3000.00ANALYST

KING5000.00PRESIDENT

TURNER1500.00SALESMAN

ADAMS1100.00CLERK

JAMES950.00CLERK

FORD3000.00ANALYST

MILLER1300.00CLERK

JONES2975.00MANAGER

BLAKE2850.00MANAGER

CLARK2450.00MANAGER

17rowsselected

SQL>selectdistinct ename,sal,jobfromemp;

ENAMESALJOB

----------------------------

WARD1250.00SALESMAN

SMITH800.00CLERK

KING5000.00PRESIDENT

TURNER1500.00SALESMAN

JAMES950.00CLERK

FORD3000.00ANALYST

CLARK2450.00MANAGER

BLAKE2850.00MANAGER

ADAMS1100.00CLERK

SCOTT3000.00ANALYST

MILLER1300.00CLERK

ALLEN1600.00SALESMAN

MARTIN1250.00SALESMAN

JONES2975.00MANAGER

14rowsselected

SQL> exits;

-the end-

你可能感兴趣的:(ora)