]]>Oracle Exceptionhttp://www.aygfsteel.com/kyleYang/archive/2010/08/03/327806.html飞熊飞熊Tue, 03 Aug 2010 03:32:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/08/03/327806.htmlhttp://www.aygfsteel.com/kyleYang/comments/327806.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/08/03/327806.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/327806.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/327806.html BEGIN ... --语句 EXCEPTION -- 例外处理 WHEN ... ... WHEN OTHERS ... END;
2) 常用预定义例?br /> EXCEPTION WHEN CURSOR_ALREADY_OPEN THEN -- ORA-06511 SQLCODE = -6511 游标已经打开 ... WHEN DUP_VAL_ON_INDEX THEN -- ORA-00001 SQLCODE = -1 向唯一索引中插入重复数?br /> ... WHEN INVALID_CURSOR THEN -- ORA-01001 SQLCODE = -1001 非法游标操作 ... WHEN INVALID_NUMBER THEN -- ORA-01722 SQLCODE = -1722 字符向数字{换失?br /> ... WHEN LOGIN_DENIED THEN -- ORA-01017 SQLCODE = -1017 ... WHEN NO_DATA_FOUND THEN -- ORA-01403 SQLCODE = +100 没有扑ֈ数据 ... WHEN NOT_LOGGED_ON THEN -- ORA-01012 SQLCODE = -1012 ... WHEN PROGRAM_ERROR THEN -- ORA-06501 SQLCODE = -6501 E序错误 ... WHEN STORAGE_ERROR THEN -- ORA-06500 SQLCODE = -6500 ... WHEN TIMEOUT_ON_RESOURCE THEN -- ORA-00051 SQLCODE = -51 ... WHEN TOO_MANY_ROWS THEN -- ORA-01422 SQLCODE = -1422 q回多行 ... WHEN TRANSACTION_BACKED_OUT THEN -- ORA-00061 SQLCODE = -61 ... WHEN VALUE_ERROR THEN -- ORA-06502 SQLCODE = -6502 数D{换错?br /> ... WHEN ZERO_DIVIDE THEN -- ORA-01476 SQLCODE = -1476 被零?br /> ... WHEN OTHERS THEN -- 其它M错误的处?br /> ... END;
3) 用户定义的例?br /> DECLARE FIND_DATA_EMP EXCEPTION; BEGIN IF ... THEN RAISE FIND_DATA_EMP; END IF; EXCEPTION WHEN LOB_NO_LOCKED THEN ... END; 4) EXCEPTION_INIT的?br /> PRAGMA EXCEPTION_INIT(例外? ORACLE错误?; 注:(x)PRAGMA 是一个编译器命o(h)Q可以认为是对编译器的一个注释?br /> 例:(x) DECLARE ZERO_DIVIDE1 EXCEPTION; PRAGMA EXCEPTION_INIT(ZERO_DIVIDE1, -1476); BEGIN ... EXCEPTION WHEN ZERO_DIVIDE1 THEN ... END;
1。INDEX UNIQUE SCAN 效率最高,主键或唯一索引 2。INDEX FULL SCAN 有顺序的输出Q不能ƈ行读索引 3。INDEX FAST FULL SCAN ȝ最块,可以q行讉K索引Q但输出不按序 4。INDEX RANGE SCAN l定的区间查? 5。INDEX SKIP SCAN 联合索引Q不同D的列,要攑֜前面
--实验后的总论? 能用唯一索引Q一定用唯一索引 能加非空Q就加非I约? 一定要l计表的信息Q烦引的信息Q柱状图的信息? 联合索引的顺序不同,影响索引的选择Q尽量将值少的放在前? 只有做到以上四点Q数据库才会(x)正确的选择执行计划? conn system/manager grant select any dictionary to scott;
conn scott/tiger drop table t1 purge; create table t1 as select * from dba_objects; analyze table t1 compute statistics; create index it1 on t1(object_type); set autot traceonly
select distinct object_type from t1; 是全表扫描Qؓ(f)什么不使用索引呢?因ؓ(f)索引中不能含有null| 如果使用索引可能生不正确的结果?
--增加非空U束 alter table t1 modify (object_type not null); select distinct object_type from t1 ; 使用INDEX FAST FULL SCAN方式查找数据
-- select object_type from t1; 使用INDEX FAST FULL SCANQ因Z需要排?
select object_type from t1 order by 1; 使用INDEX FULL SCAN,因ؓ(f)要按照顺序输?
select object_type from t1 where object_type='TABLE'; 使用INDEX RANGE SCAN
--使用非唯一索引 create index i2t1 on t1(object_id); select * from t1 where object_id=3762; 使用INDEX RANGE SCAN,因ؓ(f)数据库不知道是否唯一
--使用唯一索引 drop index i2t1; create unique index i2t1 on t1(object_id); 使用INDEX UNIQUE SCAN,因ؓ(f)数据库知道是唯一?
--跌的扫描烦? create index i3t1 on t1(object_type,object_name); select * from t1 where object_name='EMP'; select object_name from t1 where object_name='EMP'; 使用INDEX SKIP SCANQ因为数据库知道可以跌object_type,虽然object_name在第二个列?
--联合索引的顺序不同,影响索引的选择Q尽量将值少的放在前? drop index i3t1; drop index it1; create index i3t1 on t1(object_name,object_type); select * from t1 where object_type='TABLE'; 计划为全表扫描?
]]>递归写法 -- DB2http://www.aygfsteel.com/kyleYang/archive/2010/03/26/316649.html飞熊飞熊Fri, 26 Mar 2010 08:16:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/03/26/316649.htmlhttp://www.aygfsteel.com/kyleYang/comments/316649.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/03/26/316649.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/316649.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/316649.htmlAS ( SELECT ROW_ID,PARENT_MODULE_ID,MENU_NAME FROM ad_program WHERE row_id=50 UNION ALL SELECT A.ROW_ID,A.PARENT_MODULE_ID,A.MENU_NAME FROM ad_program A,TROOT B WHERE A.PARENT_MODULE_ID=B.ROW_ID ) SELECT * FROM TROOT
]]>Oracle 用户数据字典 以及(qing) 查询表字D?/title>http://www.aygfsteel.com/kyleYang/archive/2010/02/02/311599.html飞熊飞熊Tue, 02 Feb 2010 01:20:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/02/02/311599.htmlhttp://www.aygfsteel.com/kyleYang/comments/311599.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/02/02/311599.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/311599.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/311599.html查看当前用户的缺省表I间 SQL>select username,default_tablespace from user_users;
查看当前用户的角?br />SQL>select * from user_role_privs;
查看当前用户的系l权限和表权限 SQL>select * from user_sys_privs; SQL>select * from user_tab_privs;
查看用户下所有的?br />SQL>select * from user_tables;
昄用户信息(所属表I间) select default_tablespace,temporary_tablespace from dba_users where username='GAME';
1、用?br /> 查看当前用户的缺省表I间 SQL>select username,default_tablespace from user_users;
查看当前用户的角?br />SQL>select * from user_role_privs;
查看当前用户的系l权限和表权限 SQL>select * from user_sys_privs; SQL>select * from user_tab_privs;
昄当前?x)话所h的权?br />SQL>select * from session_privs;
昄指定用户所h的系l权?br />SQL>select * from dba_sys_privs where grantee='GAME';
昄Ҏ(gu)用户 select * from v$pwfile_users;
昄用户信息(所属表I间) select default_tablespace,temporary_tablespace from dba_users where username='GAME';
昄用户的PROFILE select profile from dba_users where username='GAME';
2、表
查看用户下所有的?br />SQL>select * from user_tables;
查看名称包含log字符的表 SQL>select object_name,object_id from user_objects where instr(object_name,'LOG')>0;
查看某表的创建时?br />SQL>select object_name,created from user_objects where object_name=upper('&table_name');
查看某表的大?br />SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&table_name');
查看攑֜ORACLE的内存区里的?br />SQL>select table_name,cache from user_tables where instr(cache,'Y')>0;
3、烦?br /> 查看索引个数和类?br />SQL>select index_name,index_type,table_name from user_indexes order by table_name;
查看索引被烦引的字段 SQL>select * from user_ind_columns where index_name=upper('&index_name');
查看索引的大?br />SQL>select sum(bytes)/(1024*1024) as "size(M)" from user_segments where segment_name=upper('&index_name');
4、序列号
查看序列Plast_number是当前?br />SQL>select * from user_sequences;
5、视?br /> 查看视图的名U?br />SQL>select view_name from user_views;
查看创徏视图的select语句 SQL>set view_name,text_length from user_views; SQL>set long 2000; 说明Q可以根据视囄text_lengthD定set long 的大?br />SQL>select text from user_views where view_name=upper('&view_name');
6、同义词
查看同义词的名称 SQL>select * from user_synonyms;
7、约束条?br /> 查看某表的约束条?br />SQL>select constraint_name, constraint_type,search_condition, r_constraint_name from user_constraints where table_name = upper('&table_name');
SQL>select c.constraint_name,c.constraint_type,cc.column_name from user_constraints c,user_cons_columns cc where c.owner = upper('&table_owner') and c.table_name = upper('&table_name') and c.owner = cc.owner and c.constraint_name = cc.constraint_name order by cc.position;
8、存储函数和q程
查看函数和过E的状?br />SQL>select object_name,status from user_objects where object_type='FUNCTION'; SQL>select object_name,status from user_objects where object_type='PROCEDURE';
查看函数和过E的源代?br />SQL>select text from all_source where owner=user and name=upper('&plsql_name'); --------------------------------------------------oracle 查询 表字D?以及(qing)字段 数据cd -----------------------------------------
W一U?/p>
select cname from col where tname='EMP';
W二U?/p>
select column_name from user_tab_columns where table_name='EMP';
介绍 如果查询数据的?字段数据cd
select data_type from user_tab_columns where table_name='EMP';
两个一h
select data_type,column_name from user_tab_columns where table_name='EMP';
]]>抽样表扫?/title>http://www.aygfsteel.com/kyleYang/archive/2010/02/01/311505.html飞熊飞熊Mon, 01 Feb 2010 06:32:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/02/01/311505.htmlhttp://www.aygfsteel.com/kyleYang/comments/311505.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/02/01/311505.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/311505.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/311505.htmlA表和B表joinQ我只想A表的部分数据去连接B表,此时用sample方便的多。如果用rownum需要用一个subquery?br />
--用SampleQ?/span> select A.id,A.name from A sample block(20),B where A.id=B.id --用RownumQ?/span> select A.id,A.name from (select a.id,a.name from a where rownum<100) A,B where A.id=B.id
]]>Oracle中,当误删了数据Ӟ如何恢复q些数据http://www.aygfsteel.com/kyleYang/archive/2010/01/17/309864.html飞熊飞熊Sun, 17 Jan 2010 09:14:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/01/17/309864.htmlhttp://www.aygfsteel.com/kyleYang/comments/309864.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/01/17/309864.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/309864.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/309864.html当你不小心误删了Oracle数据Ӟ怎么办,应该如何恢复q些数据Q?/p>
--好像只能恢复当日删除的数?br />create table t_hjhb_hbjcjl2 as select * from t_hjhb_hbjcjl as of timestamp to_timestamp('2009-06-10 10:28:00','YYYY-MM-DD HH24:MI:SS');
查找要恢复的数据记录Q然后再导入t_hjhb_hbjcjl表里 insert into t_hjhb_hbjcjl select * from t_hjhb_hbjcjl2
]]>DB2用一张表更新其他表的数据http://www.aygfsteel.com/kyleYang/archive/2010/01/12/309160.html飞熊飞熊Tue, 12 Jan 2010 08:09:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2010/01/12/309160.htmlhttp://www.aygfsteel.com/kyleYang/comments/309160.htmlhttp://www.aygfsteel.com/kyleYang/archive/2010/01/12/309160.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/309160.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/309160.html表结构:(x) CREATE TABLE ATEST (ID INTEGER, NAME VARCHAR(256), CODE INTEGER, NAME2 VARCHAR(256) )
CREATE TABLE BTEST (ID INTEGER, CODE INTEGER )
CREATE TABLE CTEST (ID INTEGER, NAME VARCHAR(256), NAME2 VARCHAR(256) )
SQL语句Q?br />一张表更新另一张表的字D:(x) update atest set atest.name=(select ctest.name from ctest where atest.id = ctest.id) where atest.id in (select ctest.id from ctest);
两张表关联更新另一张表的字D:(x) update atest set (name,name2) = (SELECT CASE WHEN CTEST.NAME IS NULL THEN ATEST.NAME ELSE CTEST.NAME END, CASE WHEN CTEST.NAME2 IS NULL THEN ATEST.NAME2 ELSE CTEST.NAME2 END FROM BTEST LEFT JOIN CTEST on BTEST.ID = CTEST.ID WHERE atest.CODE = BTEST.CODE) WHERE atest.CODE IN (SELECT BTEST.CODE FROM BTEST);
update student set (name,id )= (select name ,id from (select student.rowid rd,student1.name,student1.id from student1,student where student1.int_id =student.int_id) tmp where student.rowid=tmp.rd); commit;
3.
update test_a a set (a.name,a.age)= (select b.name,b.age from test_b b where a.id = b.id) where exists (select * from test_b c where c.id=a.id)
4.
UPDATE t_A SET Djrq= ( SELECT djrq FROM t_B WHERE t_A.ID = T_B.ID WHERE ROWNUM = 1 ) WHERE t_A.ID IN ( SELECT ID FROM t_B WHERE jwh='XX? )
5.
update tbl1 a set (a.col1, a.col2) = (select b.col1, b.col2 from tbl2 b where a.key = b.key) where a.key in(select key from tbl2)
set serveroutput on;
declare
varno varchar2(20);
varprice varchar2(20);
CURSOR mycur(vartype number) is
select emp_no,emp_zc from cus_emp_basic
where com_no = vartype;
begin
if mycur%isopen = false then
open mycur(000627);
end if;
fetch mycur into varno,varprice;
while mycur%found
loop
dbms_output.put_line(varno||','||varprice);
if mycur%rowcount=2 then
exit;
end if;
fetch mycur into varno,varprice;
end loop;
close mycur;
end;
set serveroutput on;
declare
type person is record
(
empno cus_emp_basic.emp_no%type,
empzc cus_emp_basic.emp_zc%type);
person1 person;
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
if mycur%isopen = false then
open mycur(000627);
end if;
loop
fetch mycur into person1;
exit when mycur%notfound;
dbms_output.put_line('雇员~号:'||person1.empno||',地址:'||person1.empzc);
end loop;
close mycur;
end;
set serveroutput on;
declare
cursor mycur(vartype number)is
select emp_no,emp_zc from cus_emp_basic
where com_no=vartype;
begin
for person in mycur(000627) loop
dbms_output.put_line('雇员~号:'||person.emp_no||',地址:'||person.emp_zc);
end loop;
end;
]]>over partition by与group by 的区?http://www.aygfsteel.com/kyleYang/archive/2009/12/03/304704.html飞熊飞熊Thu, 03 Dec 2009 14:34:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2009/12/03/304704.htmlhttp://www.aygfsteel.com/kyleYang/comments/304704.htmlhttp://www.aygfsteel.com/kyleYang/archive/2009/12/03/304704.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/304704.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/304704.html
目前我只知道一个这L(fng)区别Q?br />
比如有一张表saraly:CREATE TABLE SALARY AS SELECT 'A' NAME,10 DEPT,1000 SALARY FROM DUAL UNION ALL SELECT 'B',10,2000 FROM DUAL UNION ALL SELECT 'C' ,20,1500 FROM DUAL UNION ALL SELECT 'D',20,3000 FROM DUAL UNION ALL
SELECT 'E',10,1000 FROM DUAL;
NAME DEPT SALARY
A 10 1000
B 10 2000
C 20 1500
D 20 3000
E 10 1000
用over partition by 我就可以查询到每位员工本来的具体信息和它所在部门的d资:(x)
select name,dept,salary,sum(salary) over (partition by dept) total_salary from salary;
name dept salary tatal_salary
A 10 1000 4000
B 10 2000 4000
E 10 1000 4000
C 20 1500 4500
D 20 3000 4500
用goup by 没办法做到q点Q只能查询到每个部门的d资:(x)
select dept,sum(salary) total_salary from salary group by dept
dept total_salary
10 4000
20 4500
另外over partion by q可以做到查询每位员工占部门d资的癑ֈ比:(x)
select name,dept,salary,salary*100/sum(salary) over (partition by dept) percent from salary;
name dept salary percent
A 10 1000 25
B 10 2000 50
E 10 1000 25
C 20 1500 33.3333333333333
D 20 3000 66.6666666666667
]]>递归查询--l典http://www.aygfsteel.com/kyleYang/archive/2009/12/03/304702.html飞熊飞熊Thu, 03 Dec 2009 14:28:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2009/12/03/304702.htmlhttp://www.aygfsteel.com/kyleYang/comments/304702.htmlhttp://www.aygfsteel.com/kyleYang/archive/2009/12/03/304702.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/304702.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/304702.html
CREATE TABLE TEST.AREATB
(
ID NUMBER(6,0) NOT NULL,
AREANAME VARCHAR2(256),
PARENTAREAID NUMBER(6,0)
)
2.数据Q?br />
1
中国
-1
2
北京
1
3
安徽
1
4
合肥
3
5
宣武?/td>
2
3.SQLQ语?br />
select id, areaname, substr(sys_connect_by_path(areaname,','),2)
from AREATB bb
start with parentareaid = -1
connect by parentareaid=prior id
]]>存储q程http://www.aygfsteel.com/kyleYang/archive/2009/12/02/304528.html飞熊飞熊Wed, 02 Dec 2009 08:39:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2009/12/02/304528.htmlhttp://www.aygfsteel.com/kyleYang/comments/304528.htmlhttp://www.aygfsteel.com/kyleYang/archive/2009/12/02/304528.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/304528.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/304528.html1.EXCEPTION:
DECLARE
numerator NUMBER;
denominator NUMBER;
the_ratio NUMBER;
lower_limit CONSTANT NUMBER := 0.72;
samp_num CONSTANT NUMBER := 132;
BEGIN
SELECT x, y INTO numerator, denominator FROM result_table
WHERE sample_id = samp_num;
the_ratio := numerator/denominator;
IF the_ratio > lower_limit THEN
INSERT INTO ratio VALUES (samp_num, the_ratio);
ELSE
INSERT INTO ratio VALUES (samp_num, -1);
END IF;
COMMIT;
EXCEPTION
WHEN ZERO_DIVIDE THEN
INSERT INTO ratio VALUES (samp_num, 0);
COMMIT;
WHEN OTHERS THEN
ROLLBACK;
END;
2.
CREATE OR REPLACE PROCEDURE
get_student_major
(v_student_id IN students.student_id%TYPE,
v_name OUT students.student_name%TYPE) IS
BEGIN
SELECT student_name
INTO v_name
FROM students
WHERE student_id = v_student_id;
exception
WHEN TOO_MANY_ROWS THEN dbms_output.put_line('TMR error'); -- 行数 > 1
WHEN NO_DATA_FOUND THEN dbms_output.put_line('NDF error'); -- 行数?0
END;
3.
CREATE OR REPLACE FUNCTION
update_salaries(new_sal IN professors.salary%TYPE) -- %TYPE 某一字段的类型作为类?br />
RETURN NATURAL IS
BEGIN
UPDATE professors2 SET salary = new_sal;
RETURN SQL%ROWCOUNT; -- 判断更新的行?br />
END update_salaries;
4.
CREATE OR REPLACE PROCEDURE
add_student(rec IN OUT students%ROWTYPE) IS -- students%ROWTYPE作ؓ(f)表的某一行作为变量类?br />
BEGIN
SELECT 'A'||students_pk_seq.nextval
INTO rec.student_id
FROM dual;
INSERT INTO students (student_id, student_name,
college_major, status, state, license_no)
VALUES (rec.student_id, rec.student_name,
rec.college_major, rec.status,
rec.state, rec.license_no);
END add_student;
CURSOR c1 IS SELECT deptno, dname, loc FROM dept;
dept_rec3 c1%ROWTYPE;
BEGIN
...
dept_rec1 := dept_rec2;
However, because dept_rec2 is based on a table and dept_rec3 is based on a cursor, the following assignment is illegal:
dept_rec2 := dept_rec3; -- illegal
6.
DECLARE
CURSOR my_cursor IS
SELECT sal + NVL(comm, 0) wages, ename FROM emp;
my_rec my_cursor%ROWTYPE;
BEGIN
OPEN my_cursor;
LOOP
FETCH my_cursor INTO my_rec;
EXIT WHEN my_cursor%NOTFOUND;
IF my_rec.wages > 2000 THEN
INSERT INTO temp VALUES (NULL, my_rec.wages, my_rec.ename);
END IF;
END LOOP;
CLOSE my_cursor;
END;
7. *****
-- 包,函数Q存储过E?br />
CREATE OR REPLACE PACKAGE BODY students_pkg IS
FUNCTION add_student(rec IN students%ROWTYPE)
RETURN students.student_id%TYPE
IS
ID students.student_id%TYPE;
BEGIN
SELECT 'A'||students_pk_seq.nextval INTO ID FROM dual;
INSERT INTO students (student_id, student_name,
college_major, status, state, license_no)
VALUES (ID, rec.student_name, rec.college_major,
rec.status, rec.state, rec.license_no);
RETURN ID;
END add_student;
PROCEDURE add_vehicle(rec IN student_vehicles%ROWTYPE) IS
BEGIN
INSERT INTO student_vehicles (state, tag_no,
vehicle_desc, student_id, parking_sticker)
VALUES (rec.state, rec.tag_no,
rec.vehicle_desc, rec.student_id, rec.parking_sticker);
END add_vehicle;
END students_pkg;
8.怎样从一个过E返回一个结果集
create or replace procedure p_stu_lst(result out sys_refcursor) is
BEGIN
OPEN RESULT FOR SELECT * FROM test;
end p_stu_lst;
SQL> select * from test;
NAME KM CJ
---------- ---------- ----------
张三 语文 80
张三 数学 86
张三 p 75
李四 语文 78
李四 数学 85
李四 p 78
李四 物理 90
已选择7行?br />
SQL> exec p_stu_lst(:aaa);
PL/SQL q程已成功完成?br />
SQL> print aaa
NAME KM CJ
---------- ---------- ----------
张三 语文 80
张三 数学 86
张三 p 75
李四 语文 78
李四 数学 85
李四 p 78
李四 物理 90
2.{案如下Q?br />
select name,sum(yuwen) yuwen,sum(shuxue) shuxue,sum(huaxue) huaxue
from(
select name,
case kechen when '语文' then fengshu end yuwen,
case kechen when '数学' then fengshu end shuxue,
case kechen when '化学' then fengshu end huaxue
from table
)as t1
group by name
]]>SQL重复记录查询http://www.aygfsteel.com/kyleYang/archive/2009/12/02/304526.html飞熊飞熊Wed, 02 Dec 2009 08:33:00 GMThttp://www.aygfsteel.com/kyleYang/archive/2009/12/02/304526.htmlhttp://www.aygfsteel.com/kyleYang/comments/304526.htmlhttp://www.aygfsteel.com/kyleYang/archive/2009/12/02/304526.html#Feedback0http://www.aygfsteel.com/kyleYang/comments/commentRss/304526.htmlhttp://www.aygfsteel.com/kyleYang/services/trackbacks/304526.htmlselect * from people where peopleId in (select peopleId from people group by peopleId having count (peopleId) > 1)
2、删除表中多余的重复记录Q重复记录是Ҏ(gu)单个字段QpeopleIdQ来判断Q只留有rowid最的记录 delete from people where peopleId in (select peopleId from people group by peopleId having count (peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录Q多个字D) select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
4、删除表中多余的重复记录Q多个字D)Q只留有rowid最的记录 delete from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录Q多个字D)Q不包含rowid最的记录 select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1) and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
(? 比方?br />在A表中存在一个字D“name”, 而且不同记录之间的“name”值有可能?x)相同?br />现在是需要查询出在该表中的各记录之间Q“name”值存在重复的; Select Name,Count(*) From A Group By Name Having Count(*) > 1 如果q查性别也相同大则如? Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
(? Ҏ(gu)一 declare @max integer,@id integer declare cur_rows cursor local for select dD?count(*) from 表名 group by dD?having
count(*) >Q?1 open cur_rows fetch cur_rows into @id,@max while @@fetch_status=0 begin select @max = @max -1 set rowcount @max delete from 表名 where dD?= @id fetch cur_rows into @id,@max end close cur_rows set rowcount 0
Ҏ(gu)?/p>
有两个意义上的重复记录,一是完全重复的记录Q也x有字D均重复的记录,
二是部分关键字段重复的记录,比如Name字段重复Q而其他字D不一定重复或都重复可以忽略?/p>
1、对于第一U重复,比较Ҏ(gu)解决Q?br />select distinct * from tableName
可以得到无重复记录的结果集?/p>
如果该表需要删除重复的记录Q重复记录保?条)Q可以按以下Ҏ(gu)删除 select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp
发生q种重复的原因是表设计不周生的Q增加唯一索引列即可解冟?/p>
2、这c重复问题通常要求保留重复记录中的W一条记录,操作Ҏ(gu)如下
假设有重复的字段为Name,AddressQ要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID select * from #Tmp where autoID in(select autoID from #tmp2)
where id in (select id from tablename group by id having count(id) > 1)
(?删除重复 delete from test a where a.rowid != ( select max(b.rowid) from test b where a.id = b.id )
(?删除重复 Ҏ(gu)一Qdelete from demo a where a.rowid <> (select max(rowid) from demo b where b.object_id=a.object_id); 耗时Q几个小时以? Ҏ(gu)二:(x) delete from demo where rowid in (select rid from (select rowid rid,row_number() over(partition by object_id order by rowid) rn from demo) where rn <> 1 ); 耗时Q?0U?br style="FONT-FAMILY: " />Ҏ(gu)三:(x) create table demo2 as select object_id,owner... from (select demo.*,row_number() over(partition by object_id order by rowid) rn from demo) where rn = 1; truncate table demo; insert into demo select * from demo2; drop table demo2; p时Q?10U,适合大数据量的情况,产生更少回滚量;