SQL ɾ³ýÖØ¸´Êý¾Ý£¬Ö»±£Áô1Ìõ
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--I¡¢NameÏàͬID×îСµÄ¼Ç¼(ÍÆ¼öÓÃ1,2,3),±£Áô×îСһÌõ
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
·½·¨2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
·½·¨7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
Éú³É½á¹û:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1
(2 ÐÐÊÜÓ°Ïì)
*/
--II¡¢NameÏàͬID±£Áô×î´óµÄÒ»Ìõ¼Ç¼:
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)
·½·¨2:
delete a from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID fr
Ïà¹ØÎĵµ£º
SELECT g.rid, room_no, planed_count,cc from
(SELECT rid,room_no, planed_count from YC_HOTEL_ROOM_T ) r
left join
(SELECT rid,count(*) cc from YC_GUEST_INFO_T group by rid ) g
ON r.rid = g.rid WHERE planed_ ......
´ÓTable ±íÖÐÈ¡³öµÚ m Ìõµ½µÚ n ÌõµÄ¼Ç¼£º(Not In °æ±¾)
SELECT TOP n-m+1 *
from Table
WHERE (id NOT IN (SELECT TOP m-1 id from Table ))
--´ÓTABLE±íÖÐÈ¡³öµÚmµ½nÌõ¼Ç¼ (Exists°æ±¾)
SELECT TOP n-m+1 * from TABLE AS a WHERE Not Exists
(Select * from (Select Top m-1 * from TABLE orde ......
alert index mem_ct monitoring usage;
desc v$object_usage;
set linesize 190
select * from v$object_usage;
SQL>SET AUTOTRACE ON;
¡¡¡¡*autotrace¹¦ÄÜÖ»ÄÜÔÚSQL*PLUSÀïʹÓÃ
¡¡¡¡ÆäËûһЩʹÓ÷½·¨£º
¡¡¡¡2.2.1¡¢ÔÚSQLPLUSÖеõ½Óï¾ä×ܵÄÖ´ÐÐʱ¼ä
¡¡¡¡SQL> set timing on;
2.2.2¡¢Ö»ÏÔʾִÐмƻ®--(»áÍ¬Ê ......
ÈçºÎÅжÏ×Ö¶ÎÊÇ·ñ´æÔÚ
if col_length('±íÃû','×Ö¶Î1') is null ALTER TABLE ±íÃû ADD ×Ö¶Î1 Nvarchar(50) if col_length('±íÃû','×Ö¶Î2') is null ALTER TABLE ±íÃû ADD ×Ö¶Î2 Nvarchar(50) ");
ɾ³ý×Ö¶Î
if col_length('±íÃû','×Ö¶Î1,') is not null ALTER TABLE ±íÃû drop c ......